22114cd5be9d368a6be8e8e6d9f270d81aaf2b9c
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"
54 #include "libnr/nr-matrix-div.h"
55 #include "libnr/nr-matrix-fns.h"
56 #include "libnr/nr-matrix-scale-ops.h"
57 #include "libnr/nr-matrix-translate-ops.h"
58 #include "libnr/nr-scale-translate-ops.h"
59 #include "libnr/nr-translate-scale-ops.h"
60 #include "algorithms/find-last-if.h"
61 #include "util/reverse-list.h"
63 #include "xml/repr.h"
65 #define noSP_ITEM_DEBUG_IDLE
67 static void sp_item_class_init(SPItemClass *klass);
68 static void sp_item_init(SPItem *item);
70 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
71 static void sp_item_release(SPObject *object);
72 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
73 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
74 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
76 static gchar *sp_item_private_description(SPItem *item);
77 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
79 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
80 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
82 static SPObjectClass *parent_class;
84 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
85 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
87 /**
88 * Registers SPItem class and returns its type number.
89 */
90 GType
91 sp_item_get_type(void)
92 {
93 static GType type = 0;
94 if (!type) {
95 GTypeInfo info = {
96 sizeof(SPItemClass),
97 NULL, NULL,
98 (GClassInitFunc) sp_item_class_init,
99 NULL, NULL,
100 sizeof(SPItem),
101 16,
102 (GInstanceInitFunc) sp_item_init,
103 NULL, /* value_table */
104 };
105 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
106 }
107 return type;
108 }
110 /**
111 * SPItem vtable initialization.
112 */
113 static void
114 sp_item_class_init(SPItemClass *klass)
115 {
116 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
118 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
120 sp_object_class->build = sp_item_build;
121 sp_object_class->release = sp_item_release;
122 sp_object_class->set = sp_item_set;
123 sp_object_class->update = sp_item_update;
124 sp_object_class->write = sp_item_write;
126 klass->description = sp_item_private_description;
127 klass->snappoints = sp_item_private_snappoints;
128 }
130 /**
131 * Callback for SPItem object initialization.
132 */
133 static void
134 sp_item_init(SPItem *item)
135 {
136 item->init();
137 }
139 void SPItem::init() {
140 this->sensitive = TRUE;
142 this->transform_center_x = 0;
143 this->transform_center_y = 0;
145 this->_is_evaluated = true;
146 this->_evaluated_status = StatusUnknown;
148 this->transform = NR::identity();
150 this->display = NULL;
152 this->clip_ref = new SPClipPathReference(this);
153 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
154 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
155 _clip_ref_connection = cs1.connect(sl1);
157 this->mask_ref = new SPMaskReference(this);
158 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
159 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
160 _mask_ref_connection = cs2.connect(sl2);
162 if (!this->style) this->style = sp_style_new_from_object(this);
164 this->avoidRef = new SPAvoidRef(this);
166 new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
167 }
169 bool SPItem::isVisibleAndUnlocked() const {
170 return (!isHidden() && !isLocked());
171 }
173 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
174 return (!isHidden(display_key) && !isLocked());
175 }
177 bool SPItem::isLocked() const {
178 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
179 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
180 return true;
181 }
182 return false;
183 }
185 void SPItem::setLocked(bool locked) {
186 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
187 ( locked ? "1" : NULL ));
188 updateRepr();
189 }
191 bool SPItem::isHidden() const {
192 if (!isEvaluated())
193 return true;
194 return style->display.computed == SP_CSS_DISPLAY_NONE;
195 }
197 void SPItem::setHidden(bool hide) {
198 style->display.set = TRUE;
199 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
200 style->display.computed = style->display.value;
201 style->display.inherit = FALSE;
202 updateRepr();
203 }
205 bool SPItem::isHidden(unsigned display_key) const {
206 if (!isEvaluated())
207 return true;
208 for ( SPItemView *view(display) ; view ; view = view->next ) {
209 if ( view->key == display_key ) {
210 g_assert(view->arenaitem != NULL);
211 for ( NRArenaItem *arenaitem = view->arenaitem ;
212 arenaitem ; arenaitem = arenaitem->parent )
213 {
214 if (!arenaitem->visible) {
215 return true;
216 }
217 }
218 return false;
219 }
220 }
221 return true;
222 }
224 void SPItem::setEvaluated(bool evaluated) {
225 _is_evaluated = evaluated;
226 _evaluated_status = StatusSet;
227 }
229 void SPItem::resetEvaluated() {
230 if ( StatusCalculated == _evaluated_status ) {
231 _evaluated_status = StatusUnknown;
232 bool oldValue = _is_evaluated;
233 if ( oldValue != isEvaluated() ) {
234 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
235 }
236 } if ( StatusSet == _evaluated_status ) {
237 SPObject const *const parent = SP_OBJECT_PARENT(this);
238 if (SP_IS_SWITCH(parent)) {
239 SP_SWITCH(parent)->resetChildEvaluated();
240 }
241 }
242 }
244 bool SPItem::isEvaluated() const {
245 if ( StatusUnknown == _evaluated_status ) {
246 _is_evaluated = sp_item_evaluate(this);
247 _evaluated_status = StatusCalculated;
248 }
249 return _is_evaluated;
250 }
252 /**
253 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
254 * Corresponds to setExplicitlyHidden.
255 */
256 bool
257 SPItem::isExplicitlyHidden() const
258 {
259 return (this->style->display.set
260 && this->style->display.value == SP_CSS_DISPLAY_NONE);
261 }
263 /**
264 * Sets the display CSS property to `hidden' if \a val is true,
265 * otherwise makes it unset
266 */
267 void
268 SPItem::setExplicitlyHidden(bool const val) {
269 this->style->display.set = val;
270 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
271 this->style->display.computed = this->style->display.value;
272 this->updateRepr();
273 }
275 /**
276 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
277 */
278 void
279 SPItem::setCenter(NR::Point object_centre) {
280 NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
281 if (bbox) {
282 transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
283 if (fabs(transform_center_x) < 1e-5) // rounding error
284 transform_center_x = 0;
285 transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
286 if (fabs(transform_center_y) < 1e-5) // rounding error
287 transform_center_y = 0;
288 }
289 }
291 void
292 SPItem::unsetCenter() {
293 transform_center_x = 0;
294 transform_center_y = 0;
295 }
297 bool SPItem::isCenterSet() {
298 return (transform_center_x != 0 || transform_center_y != 0);
299 }
301 NR::Point SPItem::getCenter() {
302 NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
303 if (bbox) {
304 return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
305 } else {
306 return NR::Point (0, 0); // something's wrong!
307 }
308 }
311 namespace {
313 bool is_item(SPObject const &object) {
314 return SP_IS_ITEM(&object);
315 }
317 }
319 void SPItem::raiseToTop() {
320 using Inkscape::Algorithms::find_last_if;
322 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
323 SP_OBJECT_NEXT(this), NULL, &is_item
324 );
325 if (topmost) {
326 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
327 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
328 }
329 }
331 void SPItem::raiseOne() {
332 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
333 SP_OBJECT_NEXT(this), NULL, &is_item
334 );
335 if (next_higher) {
336 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
337 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
338 sp_repr_parent(repr)->changeOrder(repr, ref);
339 }
340 }
342 void SPItem::lowerOne() {
343 using Inkscape::Util::MutableList;
344 using Inkscape::Util::reverse_list;
346 MutableList<SPObject &> next_lower=std::find_if(
347 reverse_list<SPObject::SiblingIterator>(
348 SP_OBJECT_PARENT(this)->firstChild(), this
349 ),
350 MutableList<SPObject &>(),
351 &is_item
352 );
353 if (next_lower) {
354 ++next_lower;
355 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
356 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
357 sp_repr_parent(repr)->changeOrder(repr, ref);
358 }
359 }
361 void SPItem::lowerToBottom() {
362 using Inkscape::Algorithms::find_last_if;
363 using Inkscape::Util::MutableList;
364 using Inkscape::Util::reverse_list;
366 MutableList<SPObject &> bottom=find_last_if(
367 reverse_list<SPObject::SiblingIterator>(
368 SP_OBJECT_PARENT(this)->firstChild(), this
369 ),
370 MutableList<SPObject &>(),
371 &is_item
372 );
373 if (bottom) {
374 ++bottom;
375 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
376 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
377 sp_repr_parent(repr)->changeOrder(repr, ref);
378 }
379 }
381 static void
382 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
383 {
384 sp_object_read_attr(object, "style");
385 sp_object_read_attr(object, "transform");
386 sp_object_read_attr(object, "clip-path");
387 sp_object_read_attr(object, "mask");
388 sp_object_read_attr(object, "sodipodi:insensitive");
389 sp_object_read_attr(object, "sodipodi:nonprintable");
390 sp_object_read_attr(object, "inkscape:transform-center-x");
391 sp_object_read_attr(object, "inkscape:transform-center-y");
392 sp_object_read_attr(object, "inkscape:connector-avoid");
394 if (((SPObjectClass *) (parent_class))->build) {
395 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
396 }
397 }
399 static void
400 sp_item_release(SPObject *object)
401 {
402 SPItem *item = (SPItem *) object;
404 item->_clip_ref_connection.disconnect();
405 item->_mask_ref_connection.disconnect();
407 if (item->clip_ref) {
408 item->clip_ref->detach();
409 delete item->clip_ref;
410 item->clip_ref = NULL;
411 }
413 if (item->mask_ref) {
414 item->mask_ref->detach();
415 delete item->mask_ref;
416 item->mask_ref = NULL;
417 }
419 if (item->avoidRef) {
420 delete item->avoidRef;
421 item->avoidRef = NULL;
422 }
424 if (((SPObjectClass *) (parent_class))->release) {
425 ((SPObjectClass *) parent_class)->release(object);
426 }
428 while (item->display) {
429 nr_arena_item_unparent(item->display->arenaitem);
430 item->display = sp_item_view_list_remove(item->display, item->display);
431 }
433 item->_transformed_signal.~signal();
434 }
436 static void
437 sp_item_set(SPObject *object, unsigned key, gchar const *value)
438 {
439 SPItem *item = (SPItem *) object;
441 switch (key) {
442 case SP_ATTR_TRANSFORM: {
443 NR::Matrix t;
444 if (value && sp_svg_transform_read(value, &t)) {
445 sp_item_set_item_transform(item, t);
446 } else {
447 sp_item_set_item_transform(item, NR::identity());
448 }
449 break;
450 }
451 case SP_PROP_CLIP_PATH: {
452 gchar *uri = Inkscape::parse_css_url(value);
453 if (uri) {
454 try {
455 item->clip_ref->attach(Inkscape::URI(uri));
456 } catch (Inkscape::BadURIException &e) {
457 g_warning("%s", e.what());
458 item->clip_ref->detach();
459 }
460 g_free(uri);
461 } else {
462 item->clip_ref->detach();
463 }
465 break;
466 }
467 case SP_PROP_MASK: {
468 gchar *uri=Inkscape::parse_css_url(value);
469 if (uri) {
470 try {
471 item->mask_ref->attach(Inkscape::URI(uri));
472 } catch (Inkscape::BadURIException &e) {
473 g_warning("%s", e.what());
474 item->mask_ref->detach();
475 }
476 g_free(uri);
477 } else {
478 item->mask_ref->detach();
479 }
481 break;
482 }
483 case SP_ATTR_SODIPODI_INSENSITIVE:
484 item->sensitive = !value;
485 for (SPItemView *v = item->display; v != NULL; v = v->next) {
486 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
487 }
488 break;
489 case SP_ATTR_STYLE:
490 sp_style_read_from_object(object->style, object);
491 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
492 break;
493 case SP_ATTR_CONNECTOR_AVOID:
494 item->avoidRef->setAvoid(value);
495 break;
496 case SP_ATTR_TRANSFORM_CENTER_X:
497 if (value) {
498 item->transform_center_x = g_strtod(value, NULL);
499 } else {
500 item->transform_center_x = 0;
501 }
502 break;
503 case SP_ATTR_TRANSFORM_CENTER_Y:
504 if (value) {
505 item->transform_center_y = g_strtod(value, NULL);
506 } else {
507 item->transform_center_y = 0;
508 }
509 break;
510 case SP_PROP_SYSTEM_LANGUAGE:
511 case SP_PROP_REQUIRED_FEATURES:
512 case SP_PROP_REQUIRED_EXTENSIONS:
513 {
514 item->resetEvaluated();
515 // pass to default handler
516 }
517 default:
518 if (SP_ATTRIBUTE_IS_CSS(key)) {
519 sp_style_read_from_object(object->style, object);
520 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
521 } else {
522 if (((SPObjectClass *) (parent_class))->set) {
523 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
524 }
525 }
526 break;
527 }
528 }
530 static void
531 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
532 {
533 if (old_clip) {
534 SPItemView *v;
535 /* Hide clippath */
536 for (v = item->display; v != NULL; v = v->next) {
537 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
538 nr_arena_item_set_clip(v->arenaitem, NULL);
539 }
540 }
541 if (SP_IS_CLIPPATH(clip)) {
542 NRRect bbox;
543 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
544 for (SPItemView *v = item->display; v != NULL; v = v->next) {
545 if (!v->arenaitem->key) {
546 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
547 }
548 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
549 NR_ARENA_ITEM_ARENA(v->arenaitem),
550 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
551 nr_arena_item_set_clip(v->arenaitem, ai);
552 nr_arena_item_unref(ai);
553 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
554 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
555 }
556 }
557 }
559 static void
560 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
561 {
562 if (old_mask) {
563 /* Hide mask */
564 for (SPItemView *v = item->display; v != NULL; v = v->next) {
565 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
566 nr_arena_item_set_mask(v->arenaitem, NULL);
567 }
568 }
569 if (SP_IS_MASK(mask)) {
570 NRRect bbox;
571 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
572 for (SPItemView *v = item->display; v != NULL; v = v->next) {
573 if (!v->arenaitem->key) {
574 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
575 }
576 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
577 NR_ARENA_ITEM_ARENA(v->arenaitem),
578 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
579 nr_arena_item_set_mask(v->arenaitem, ai);
580 nr_arena_item_unref(ai);
581 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
582 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
583 }
584 }
585 }
587 static void
588 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
589 {
590 SPItem *item = SP_ITEM(object);
592 if (((SPObjectClass *) (parent_class))->update)
593 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
595 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
596 if (flags & SP_OBJECT_MODIFIED_FLAG) {
597 for (SPItemView *v = item->display; v != NULL; v = v->next) {
598 nr_arena_item_set_transform(v->arenaitem, item->transform);
599 }
600 }
602 SPClipPath *clip_path = item->clip_ref->getObject();
603 SPMask *mask = item->mask_ref->getObject();
605 if ( clip_path || mask ) {
606 NRRect bbox;
607 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
608 if (clip_path) {
609 for (SPItemView *v = item->display; v != NULL; v = v->next) {
610 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
611 }
612 }
613 if (mask) {
614 for (SPItemView *v = item->display; v != NULL; v = v->next) {
615 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
616 }
617 }
618 }
620 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
621 for (SPItemView *v = item->display; v != NULL; v = v->next) {
622 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
623 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
624 }
625 }
626 }
628 // Update libavoid with item geometry (for connector routing).
629 item->avoidRef->handleSettingChange();
630 }
632 static Inkscape::XML::Node *
633 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
634 {
635 SPItem *item = SP_ITEM(object);
637 gchar *c = sp_svg_transform_write(item->transform);
638 repr->setAttribute("transform", c);
639 g_free(c);
641 SPObject const *const parent = SP_OBJECT_PARENT(object);
642 /** \todo Can someone please document why this is conditional on having
643 * a parent? The only parentless thing I can think of is the top-level
644 * <svg> element (SPRoot). SPRoot is derived from SPGroup, and can have
645 * style. I haven't looked at callers.
646 */
647 if (parent) {
648 SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
649 if (obj_style) {
650 gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
651 repr->setAttribute("style", ( *s ? s : NULL ));
652 g_free(s);
653 } else {
654 /** \todo I'm not sure what to do in this case. Bug #1165868
655 * suggests that it can arise, but the submitter doesn't know
656 * how to do so reliably. The main two options are either
657 * leave repr's style attribute unchanged, or explicitly clear it.
658 * Must also consider what to do with property attributes for
659 * the element; see below.
660 */
661 char const *style_str = repr->attribute("style");
662 if (!style_str) {
663 style_str = "NULL";
664 }
665 g_warning("Item's style is NULL; repr style attribute is %s", style_str);
666 }
668 /** \note We treat object->style as authoritative. Its effects have
669 * been written to the style attribute above; any properties that are
670 * unset we take to be deliberately unset (e.g. so that clones can
671 * override the property).
672 *
673 * Note that the below has an undesirable consequence of changing the
674 * appearance on renderers that lack CSS support (e.g. SVG tiny);
675 * possibly we should write property attributes instead of a style
676 * attribute.
677 */
678 sp_style_unset_property_attrs (object);
679 }
681 if (flags & SP_OBJECT_WRITE_EXT) {
682 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
683 if (item->transform_center_x != 0)
684 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
685 else
686 repr->setAttribute ("inkscape:transform-center-x", NULL);
687 if (item->transform_center_y != 0)
688 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
689 else
690 repr->setAttribute ("inkscape:transform-center-y", NULL);
691 }
693 if (item->clip_ref->getObject()) {
694 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
695 repr->setAttribute ("clip-path", value);
696 g_free ((void *) value);
697 }
698 if (item->mask_ref->getObject()) {
699 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
700 repr->setAttribute ("mask", value);
701 g_free ((void *) value);
702 }
704 if (((SPObjectClass *) (parent_class))->write) {
705 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
706 }
708 return repr;
709 }
711 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
712 SPItem::BBoxType type,
713 unsigned int dkey) const
714 {
715 NRRect r;
716 sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
717 return r;
718 }
720 void
721 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear)
722 {
723 sp_item_invoke_bbox_full(item, bbox, transform, 0, clear);
724 }
726 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
727 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
728 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
729 * clones), in turn, call this function in their bbox methods. */
730 void
731 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
732 {
733 g_assert(item != NULL);
734 g_assert(SP_IS_ITEM(item));
735 g_assert(bbox != NULL);
737 if (clear) {
738 bbox->x0 = bbox->y0 = 1e18;
739 bbox->x1 = bbox->y1 = -1e18;
740 }
742 NRRect this_bbox;
743 this_bbox.x0 = this_bbox.y0 = 1e18;
744 this_bbox.x1 = this_bbox.y1 = -1e18;
746 // call the subclass method
747 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
748 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
749 }
751 // crop the bbox by clip path, if any
752 if (item->clip_ref->getObject()) {
753 NRRect b;
754 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
755 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
756 }
758 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
759 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
760 nr_rect_d_union (bbox, bbox, &this_bbox);
761 }
762 }
764 unsigned sp_item_pos_in_parent(SPItem *item)
765 {
766 g_assert(item != NULL);
767 g_assert(SP_IS_ITEM(item));
769 SPObject *parent = SP_OBJECT_PARENT(item);
770 g_assert(parent != NULL);
771 g_assert(SP_IS_OBJECT(parent));
773 SPObject *object = SP_OBJECT(item);
775 unsigned pos=0;
776 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
777 if ( iter == object ) {
778 return pos;
779 }
780 if (SP_IS_ITEM(iter)) {
781 pos++;
782 }
783 }
785 g_assert_not_reached();
786 return 0;
787 }
789 void
790 sp_item_bbox_desktop(SPItem *item, NRRect *bbox)
791 {
792 g_assert(item != NULL);
793 g_assert(SP_IS_ITEM(item));
794 g_assert(bbox != NULL);
796 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE);
797 }
799 NR::Rect sp_item_bbox_desktop(SPItem *item)
800 {
801 NRRect ret;
802 sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE);
803 NR::Maybe<NR::Rect> result = ret.upgrade();
804 if (result) {
805 return *result;
806 } else {
807 // FIXME
808 return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
809 }
810 }
812 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
813 {
814 NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
815 /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
816 support angled guide lines. */
818 if (bbox) {
819 *p = bbox->min();
820 *p = bbox->max();
821 }
822 }
824 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
825 {
826 g_assert (item != NULL);
827 g_assert (SP_IS_ITEM(item));
829 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
830 if (item_class.snappoints) {
831 item_class.snappoints(item, p);
832 }
833 }
835 void
836 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
837 {
838 if (!item->isHidden()) {
839 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
840 if (!item->transform.test_identity()
841 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
842 {
843 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
844 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
845 sp_print_release(ctx);
846 } else {
847 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
848 }
849 }
850 }
851 }
853 static gchar *
854 sp_item_private_description(SPItem *item)
855 {
856 return g_strdup(_("Object"));
857 }
859 /**
860 * Returns a string suitable for status bar, formatted in pango markup language.
861 *
862 * Must be freed by caller.
863 */
864 gchar *
865 sp_item_description(SPItem *item)
866 {
867 g_assert(item != NULL);
868 g_assert(SP_IS_ITEM(item));
870 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
871 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
872 if (s && item->clip_ref->getObject()) {
873 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
874 g_free (s);
875 s = snew;
876 }
877 if (s && item->mask_ref->getObject()) {
878 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
879 g_free (s);
880 s = snew;
881 }
882 return s;
883 }
885 g_assert_not_reached();
886 return NULL;
887 }
889 /**
890 * Allocates unique integer keys.
891 * \param numkeys Number of keys required.
892 * \return First allocated key; hence if the returned key is n
893 * you can use n, n + 1, ..., n + (numkeys - 1)
894 */
895 unsigned
896 sp_item_display_key_new(unsigned numkeys)
897 {
898 static unsigned dkey = 0;
900 dkey += numkeys;
902 return dkey - numkeys;
903 }
905 NRArenaItem *
906 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
907 {
908 g_assert(item != NULL);
909 g_assert(SP_IS_ITEM(item));
910 g_assert(arena != NULL);
911 g_assert(NR_IS_ARENA(arena));
913 NRArenaItem *ai = NULL;
914 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
915 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
916 }
918 if (ai != NULL) {
919 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
920 nr_arena_item_set_transform(ai, item->transform);
921 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
922 nr_arena_item_set_visible(ai, !item->isHidden());
923 nr_arena_item_set_sensitive(ai, item->sensitive);
924 if (item->clip_ref->getObject()) {
925 SPClipPath *cp = item->clip_ref->getObject();
927 if (!item->display->arenaitem->key) {
928 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
929 }
930 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
932 // Show and set clip
933 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
934 nr_arena_item_set_clip(ai, ac);
935 nr_arena_item_unref(ac);
937 // Update bbox, in case the clip uses bbox units
938 NRRect bbox;
939 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
940 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
941 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
942 }
943 if (item->mask_ref->getObject()) {
944 SPMask *mask = item->mask_ref->getObject();
946 if (!item->display->arenaitem->key) {
947 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
948 }
949 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
951 // Show and set mask
952 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
953 nr_arena_item_set_mask(ai, ac);
954 nr_arena_item_unref(ac);
956 // Update bbox, in case the mask uses bbox units
957 NRRect bbox;
958 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
959 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
960 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
961 }
962 NR_ARENA_ITEM_SET_DATA(ai, item);
963 }
965 return ai;
966 }
968 void
969 sp_item_invoke_hide(SPItem *item, unsigned key)
970 {
971 g_assert(item != NULL);
972 g_assert(SP_IS_ITEM(item));
974 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
975 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
976 }
978 SPItemView *ref = NULL;
979 SPItemView *v = item->display;
980 while (v != NULL) {
981 SPItemView *next = v->next;
982 if (v->key == key) {
983 if (item->clip_ref->getObject()) {
984 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
985 nr_arena_item_set_clip(v->arenaitem, NULL);
986 }
987 if (item->mask_ref->getObject()) {
988 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
989 nr_arena_item_set_mask(v->arenaitem, NULL);
990 }
991 if (!ref) {
992 item->display = v->next;
993 } else {
994 ref->next = v->next;
995 }
996 nr_arena_item_unparent(v->arenaitem);
997 nr_arena_item_unref(v->arenaitem);
998 g_free(v);
999 } else {
1000 ref = v;
1001 }
1002 v = next;
1003 }
1004 }
1006 // Adjusters
1008 void
1009 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1010 {
1011 SPStyle *style = SP_OBJECT_STYLE (item);
1013 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1014 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1015 if (SP_IS_PATTERN (server)) {
1016 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1017 sp_pattern_transform_multiply (pattern, postmul, set);
1018 }
1019 }
1021 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1022 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1023 if (SP_IS_PATTERN (server)) {
1024 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1025 sp_pattern_transform_multiply (pattern, postmul, set);
1026 }
1027 }
1029 }
1031 void
1032 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1033 {
1034 SPStyle *style = SP_OBJECT_STYLE (item);
1036 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1037 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1038 if (SP_IS_GRADIENT (server)) {
1040 /**
1041 * \note Bbox units for a gradient are generally a bad idea because
1042 * with them, you cannot preserve the relative position of the
1043 * object and its gradient after rotation or skew. So now we
1044 * convert them to userspace units which are easy to keep in sync
1045 * just by adding the object's transform to gradientTransform.
1046 * \todo FIXME: convert back to bbox units after transforming with
1047 * the item, so as to preserve the original units.
1048 */
1049 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1051 sp_gradient_transform_multiply (gradient, postmul, set);
1052 }
1053 }
1055 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1056 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1057 if (SP_IS_GRADIENT (server)) {
1058 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1059 sp_gradient_transform_multiply (gradient, postmul, set);
1060 }
1061 }
1062 }
1064 void
1065 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1066 {
1067 SPStyle *style = SP_OBJECT_STYLE (item);
1069 if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1071 style->stroke_width.computed *= ex;
1072 style->stroke_width.set = TRUE;
1074 if (style->stroke_dash.n_dash != 0) {
1075 int i;
1076 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1077 style->stroke_dash.dash[i] *= ex;
1078 }
1079 style->stroke_dash.offset *= ex;
1080 }
1082 SP_OBJECT(item)->updateRepr();
1083 }
1084 }
1086 /**
1087 * Find out the inverse of previous transform of an item (from its repr)
1088 */
1089 NR::Matrix
1090 sp_item_transform_repr (SPItem *item)
1091 {
1092 NR::Matrix t_old(NR::identity());
1093 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1094 if (t_attr) {
1095 NR::Matrix t;
1096 if (sp_svg_transform_read(t_attr, &t)) {
1097 t_old = t;
1098 }
1099 }
1101 return t_old;
1102 }
1105 /**
1106 * Recursively scale stroke width in \a item and its children by \a expansion.
1107 */
1108 void
1109 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1110 {
1111 sp_item_adjust_stroke (item, expansion);
1113 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1114 if (SP_IS_ITEM(o))
1115 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1116 }
1117 }
1119 /**
1120 * Recursively adjust rx and ry of rects.
1121 */
1122 void
1123 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1124 {
1125 if (SP_IS_RECT (item)) {
1126 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1127 }
1129 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1130 if (SP_IS_ITEM(o))
1131 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1132 }
1133 }
1135 /**
1136 * Recursively compensate pattern or gradient transform.
1137 */
1138 void
1139 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1140 {
1141 // A clone must not touch the style (it's that of its parent!) and has no children, so quit now
1142 if (item && SP_IS_USE(item))
1143 return;
1145 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1146 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1147 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1148 NR::Matrix t_item = sp_item_transform_repr (item);
1149 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1151 if (is_pattern)
1152 sp_item_adjust_pattern (item, paint_delta);
1153 else
1154 sp_item_adjust_gradient (item, paint_delta);
1156 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation
1157 if (item && SP_IS_TEXT(item))
1158 return;
1160 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1161 if (SP_IS_ITEM(o)) {
1162 // At the level of the transformed item, t_ancestors is identity;
1163 // below it, it is the accmmulated chain of transforms from this level to the top level
1164 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1165 }
1166 }
1167 }
1169 /**
1170 * A temporary wrapper for the next function accepting the NRMatrix
1171 * instead of NR::Matrix
1172 */
1173 void
1174 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1175 {
1176 if (transform == NULL)
1177 sp_item_write_transform(item, repr, NR::identity(), adv);
1178 else
1179 sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1180 }
1182 /**
1183 * Set a new transform on an object.
1184 *
1185 * Compensate for stroke scaling and gradient/pattern fill transform, if
1186 * necessary. Call the object's set_transform method if transforms are
1187 * stored optimized. Send _transformed_signal. Invoke _write method so that
1188 * the repr is updated with the new transform.
1189 */
1190 void
1191 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1192 {
1193 g_return_if_fail(item != NULL);
1194 g_return_if_fail(SP_IS_ITEM(item));
1195 g_return_if_fail(repr != NULL);
1197 // calculate the relative transform, if not given by the adv attribute
1198 NR::Matrix advertized_transform;
1199 if (adv != NULL) {
1200 advertized_transform = *adv;
1201 } else {
1202 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1203 }
1205 if (compensate) {
1207 // recursively compensate for stroke scaling, depending on user preference
1208 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1209 double const expansion = 1. / NR::expansion(advertized_transform);
1210 sp_item_adjust_stroke_width_recursive(item, expansion);
1211 }
1213 // recursively compensate rx/ry of a rect if requested
1214 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1215 sp_item_adjust_rects_recursive(item, advertized_transform);
1216 }
1218 // recursively compensate pattern fill if it's not to be transformed
1219 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1220 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1221 }
1222 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1223 /// recursively compensate gradient fill if it's not to be transformed
1224 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1225 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1226 } else {
1227 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1228 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1229 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1230 }
1232 } // endif(compensate)
1234 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1235 NR::Matrix transform_attr (transform);
1236 if ( // run the object's set_transform (i.e. embed transform) only if:
1237 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1238 !preserve && // user did not chose to preserve all transforms
1239 !item->clip_ref->getObject() && // the object does not have a clippath
1240 !item->mask_ref->getObject() && // the object does not have a mask
1241 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.filter)
1242 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1243 ) {
1244 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1245 }
1246 sp_item_set_item_transform(item, transform_attr);
1248 // Note: updateRepr comes before emitting the transformed signal since
1249 // it causes clone SPUse's copy of the original object to brought up to
1250 // date with the original. Otherwise, sp_use_bbox returns incorrect
1251 // values if called in code handling the transformed signal.
1252 SP_OBJECT(item)->updateRepr();
1254 // send the relative transform with a _transformed_signal
1255 item->_transformed_signal.emit(&advertized_transform, item);
1256 }
1258 gint
1259 sp_item_event(SPItem *item, SPEvent *event)
1260 {
1261 g_return_val_if_fail(item != NULL, FALSE);
1262 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1263 g_return_val_if_fail(event != NULL, FALSE);
1265 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1266 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1268 return FALSE;
1269 }
1271 /**
1272 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1273 * gradients, patterns as sp_item_write_transform does.
1274 */
1275 void
1276 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1277 {
1278 g_return_if_fail(item != NULL);
1279 g_return_if_fail(SP_IS_ITEM(item));
1281 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1282 item->transform = transform;
1283 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1284 transformation. It's apparently not used anywhere else. */
1285 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1286 sp_item_rm_unsatisfied_cns(*item);
1287 }
1288 }
1291 /**
1292 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1293 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1294 */
1295 NR::Matrix
1296 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1297 NR::Matrix ret(NR::identity());
1298 g_return_val_if_fail(object != NULL, ret);
1300 /* stop at first non-renderable ancestor */
1301 while ( object != ancestor && SP_IS_ITEM(object) ) {
1302 if (SP_IS_ROOT(object)) {
1303 ret *= SP_ROOT(object)->c2p;
1304 }
1305 ret *= SP_ITEM(object)->transform;
1306 object = SP_OBJECT_PARENT(object);
1307 }
1308 return ret;
1309 }
1311 NR::Matrix
1312 i2i_affine(SPObject const *src, SPObject const *dest) {
1313 g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1314 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1315 return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1316 }
1318 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1319 return i2i_affine(this, dest);
1320 }
1322 /**
1323 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1324 * \pre (item != NULL) and SP_IS_ITEM(item).
1325 */
1326 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1327 {
1328 return i2anc_affine(item, NULL);
1329 }
1331 /**
1332 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1333 * Used in path operations mostly.
1334 * \pre (item != NULL) and SP_IS_ITEM(item).
1335 */
1336 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1337 {
1338 g_assert(item != NULL);
1339 g_assert(SP_IS_ITEM(item));
1341 NR::Matrix ret(NR::identity());
1342 g_assert(ret.test_identity());
1343 while ( NULL != SP_OBJECT_PARENT(item) ) {
1344 ret *= item->transform;
1345 item = SP_ITEM(SP_OBJECT_PARENT(item));
1346 }
1347 g_assert(SP_IS_ROOT(item));
1349 ret *= item->transform;
1351 return ret;
1352 }
1354 /* fixme: This is EVIL!!! */
1356 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1357 {
1358 g_assert(item != NULL);
1359 g_assert(SP_IS_ITEM(item));
1361 NR::Matrix const ret( sp_item_i2doc_affine(item)
1362 * NR::scale(1, -1)
1363 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1364 return ret;
1365 }
1367 // same as i2d but with i2root instead of i2doc
1368 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1369 {
1370 g_assert(item != NULL);
1371 g_assert(SP_IS_ITEM(item));
1373 NR::Matrix const ret( sp_item_i2root_affine(item)
1374 * NR::scale(1, -1)
1375 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1376 return ret;
1377 }
1379 /**
1380 * Converts a matrix \a m into the desktop coords of the \a item.
1381 * Will become a noop when we eliminate the coordinate flipping.
1382 */
1383 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1384 {
1385 NR::Matrix const ret(m
1386 * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1387 * NR::scale(1, -1));
1388 return ret;
1389 }
1391 /**
1392 * Converts a matrix \a m from the desktop coords of the \a item.
1393 * Will become a noop when we eliminate the coordinate flipping.
1394 */
1395 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1396 {
1397 NR::Matrix const ret(NR::scale(1, -1)
1398 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1399 * m);
1400 return ret;
1401 }
1403 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1404 {
1405 g_return_if_fail( item != NULL );
1406 g_return_if_fail( SP_IS_ITEM(item) );
1408 NR::Matrix dt2p; /* desktop to item parent transform */
1409 if (SP_OBJECT_PARENT(item)) {
1410 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1411 } else {
1412 dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1413 * NR::scale(1, -1) );
1414 }
1416 NR::Matrix const i2p( i2dt * dt2p );
1417 sp_item_set_item_transform(item, i2p);
1418 }
1421 NR::Matrix
1422 sp_item_dt2i_affine(SPItem const *item)
1423 {
1424 /* fixme: Implement the right way (Lauris) */
1425 return sp_item_i2d_affine(item).inverse();
1426 }
1428 /* Item views */
1430 static SPItemView *
1431 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1432 {
1433 SPItemView *new_view;
1435 g_assert(item != NULL);
1436 g_assert(SP_IS_ITEM(item));
1437 g_assert(arenaitem != NULL);
1438 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1440 new_view = g_new(SPItemView, 1);
1442 new_view->next = list;
1443 new_view->flags = flags;
1444 new_view->key = key;
1445 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1447 return new_view;
1448 }
1450 static SPItemView *
1451 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1452 {
1453 if (view == list) {
1454 list = list->next;
1455 } else {
1456 SPItemView *prev;
1457 prev = list;
1458 while (prev->next != view) prev = prev->next;
1459 prev->next = view->next;
1460 }
1462 nr_arena_item_unref(view->arenaitem);
1463 g_free(view);
1465 return list;
1466 }
1468 /**
1469 * Return the arenaitem corresponding to the given item in the display
1470 * with the given key
1471 */
1472 NRArenaItem *
1473 sp_item_get_arenaitem(SPItem *item, unsigned key)
1474 {
1475 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1476 if ( iv->key == key ) {
1477 return iv->arenaitem;
1478 }
1479 }
1481 return NULL;
1482 }
1484 int
1485 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1486 {
1487 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1488 SP_OBJECT_REPR(second));
1489 }
1491 SPItem *
1492 sp_item_first_item_child (SPObject *obj)
1493 {
1494 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1495 if (SP_IS_ITEM (iter))
1496 return SP_ITEM (iter);
1497 }
1498 return NULL;
1499 }
1502 /*
1503 Local Variables:
1504 mode:c++
1505 c-file-style:"stroustrup"
1506 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1507 indent-tabs-mode:nil
1508 fill-column:99
1509 End:
1510 */
1511 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :