sp-item.cpp revision 34521350d3cff94d8cf939eceb15d17dc05b52b8
691N/A/** \file
691N/A * Base class for visual SVG elements
691N/A */
691N/A/*
691N/A * Authors:
691N/A * Lauris Kaplinski <lauris@kaplinski.com>
691N/A * bulia byak <buliabyak@users.sf.net>
691N/A * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
691N/A * Abhishek Sharma
691N/A * Jon A. Cruz <jon@joncruz.org>
691N/A *
691N/A * Copyright (C) 2001-2006 authors
691N/A * Copyright (C) 2001 Ximian, Inc.
691N/A *
691N/A * Released under GNU GPL, read the file 'COPYING' for more information
691N/A */
691N/A
691N/A/** \class SPItem
691N/A *
691N/A * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
691N/A * is a subclass of SPObject, with great deal of specific functionality.
691N/A */
691N/A
691N/A#ifdef HAVE_CONFIG_H
691N/A# include "config.h"
691N/A#endif
691N/A
691N/A#include "sp-item.h"
691N/A#include "svg/svg.h"
691N/A#include "print.h"
691N/A#include "display/drawing-item.h"
691N/A#include "attributes.h"
691N/A#include "document.h"
691N/A#include "uri.h"
691N/A#include "inkscape.h"
691N/A#include "desktop.h"
691N/A#include "desktop-handles.h"
691N/A
691N/A#include "style.h"
691N/A#include <glibmm/i18n.h>
691N/A#include "sp-root.h"
691N/A#include "sp-clippath.h"
691N/A#include "sp-mask.h"
691N/A#include "sp-rect.h"
691N/A#include "sp-use.h"
691N/A#include "sp-text.h"
691N/A#include "sp-item-rm-unsatisfied-cns.h"
691N/A#include "sp-pattern.h"
691N/A#include "sp-paint-server.h"
691N/A#include "sp-switch.h"
691N/A#include "sp-guide-constraint.h"
691N/A#include "gradient-chemistry.h"
691N/A#include "preferences.h"
691N/A#include "conn-avoid-ref.h"
691N/A#include "conditions.h"
691N/A#include "sp-filter-reference.h"
691N/A#include "filter-chemistry.h"
691N/A#include "sp-guide.h"
691N/A#include "sp-title.h"
691N/A#include "sp-desc.h"
691N/A
691N/A#include "util/find-last-if.h"
691N/A#include "util/reverse-list.h"
691N/A#include <2geom/rect.h>
691N/A#include <2geom/affine.h>
691N/A#include <2geom/transforms.h>
691N/A
691N/A#include "xml/repr.h"
691N/A#include "extract-uri.h"
691N/A#include "helper/geom.h"
691N/A
691N/A#include "live_effects/lpeobject.h"
691N/A#include "live_effects/effect.h"
691N/A#include "live_effects/lpeobject-reference.h"
691N/A
691N/A#define noSP_ITEM_DEBUG_IDLE
691N/A
691N/ASPObjectClass * SPItemClass::static_parent_class=0;
691N/A
691N/A/**
691N/A * Registers SPItem class and returns its type number.
691N/A */
691N/AGType
691N/ASPItem::getType(void)
691N/A{
691N/A static GType type = 0;
691N/A if (!type) {
691N/A GTypeInfo info = {
691N/A sizeof(SPItemClass),
691N/A NULL, NULL,
691N/A (GClassInitFunc) SPItemClass::sp_item_class_init,
691N/A NULL, NULL,
691N/A sizeof(SPItem),
691N/A 16,
691N/A (GInstanceInitFunc) sp_item_init,
691N/A NULL, /* value_table */
691N/A };
691N/A type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
691N/A }
691N/A return type;
691N/A}
691N/A
691N/A/**
691N/A * SPItem vtable initialization.
691N/A */
691N/Avoid
691N/ASPItemClass::sp_item_class_init(SPItemClass *klass)
691N/A{
691N/A SPObjectClass *sp_object_class = (SPObjectClass *) klass;
691N/A
691N/A static_parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
691N/A
691N/A sp_object_class->build = SPItem::sp_item_build;
691N/A sp_object_class->release = SPItem::sp_item_release;
691N/A sp_object_class->set = SPItem::sp_item_set;
691N/A sp_object_class->update = SPItem::sp_item_update;
691N/A sp_object_class->write = SPItem::sp_item_write;
691N/A
691N/A klass->description = SPItem::sp_item_private_description;
691N/A klass->snappoints = SPItem::sp_item_private_snappoints;
691N/A}
691N/A
691N/A/**
691N/A * Callback for SPItem object initialization.
691N/A */
691N/Avoid SPItem::sp_item_init(SPItem *item)
691N/A{
691N/A item->init();
691N/A}
691N/A
691N/Avoid SPItem::init() {
691N/A sensitive = TRUE;
691N/A bbox_valid = FALSE;
691N/A
691N/A transform_center_x = 0;
691N/A transform_center_y = 0;
691N/A
691N/A _is_evaluated = true;
691N/A _evaluated_status = StatusUnknown;
691N/A
691N/A transform = Geom::identity();
691N/A doc_bbox = Geom::OptRect();
691N/A freeze_stroke_width = false;
691N/A
691N/A display = NULL;
691N/A
691N/A clip_ref = new SPClipPathReference(this);
691N/A clip_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(clip_ref_changed), this));
691N/A
691N/A mask_ref = new SPMaskReference(this);
691N/A mask_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(mask_ref_changed), this));
691N/A
691N/A avoidRef = new SPAvoidRef(this);
691N/A
691N/A new (&constraints) std::vector<SPGuideConstraint>();
691N/A
691N/A new (&_transformed_signal) sigc::signal<void, Geom::Affine const *, SPItem *>();
691N/A}
691N/A
691N/Abool SPItem::isVisibleAndUnlocked() const {
691N/A return (!isHidden() && !isLocked());
691N/A}
691N/A
691N/Abool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
691N/A return (!isHidden(display_key) && !isLocked());
691N/A}
691N/A
691N/Abool SPItem::isLocked() const {
691N/A for (SPObject const *o = this; o != NULL; o = o->parent) {
691N/A if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive)) {
691N/A return true;
691N/A }
691N/A }
691N/A return false;
691N/A}
691N/A
691N/Avoid SPItem::setLocked(bool locked) {
691N/A setAttribute("sodipodi:insensitive",
691N/A ( locked ? "1" : NULL ));
691N/A updateRepr();
691N/A}
691N/A
691N/Abool SPItem::isHidden() const {
691N/A if (!isEvaluated())
691N/A return true;
691N/A return style->display.computed == SP_CSS_DISPLAY_NONE;
691N/A}
691N/A
691N/Avoid SPItem::setHidden(bool hide) {
691N/A style->display.set = TRUE;
691N/A style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
691N/A style->display.computed = style->display.value;
691N/A style->display.inherit = FALSE;
691N/A updateRepr();
691N/A}
691N/A
691N/Abool SPItem::isHidden(unsigned display_key) const {
691N/A if (!isEvaluated())
691N/A return true;
691N/A for ( SPItemView *view(display) ; view ; view = view->next ) {
691N/A if ( view->key == display_key ) {
691N/A g_assert(view->arenaitem != NULL);
691N/A for ( Inkscape::DrawingItem *arenaitem = view->arenaitem ;
691N/A arenaitem ; arenaitem = arenaitem->parent() )
691N/A {
691N/A if (!arenaitem->visible()) {
691N/A return true;
691N/A }
691N/A }
691N/A return false;
691N/A }
691N/A }
691N/A return true;
691N/A}
691N/A
691N/Avoid SPItem::setEvaluated(bool evaluated) {
691N/A _is_evaluated = evaluated;
691N/A _evaluated_status = StatusSet;
691N/A}
691N/A
691N/Avoid SPItem::resetEvaluated() {
691N/A if ( StatusCalculated == _evaluated_status ) {
691N/A _evaluated_status = StatusUnknown;
691N/A bool oldValue = _is_evaluated;
691N/A if ( oldValue != isEvaluated() ) {
691N/A requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
691N/A }
691N/A } if ( StatusSet == _evaluated_status ) {
691N/A if (SP_IS_SWITCH(parent)) {
691N/A SP_SWITCH(parent)->resetChildEvaluated();
691N/A }
691N/A }
691N/A}
691N/A
691N/Abool SPItem::isEvaluated() const {
691N/A if ( StatusUnknown == _evaluated_status ) {
691N/A _is_evaluated = sp_item_evaluate(this);
691N/A _evaluated_status = StatusCalculated;
691N/A }
691N/A return _is_evaluated;
691N/A}
691N/A
691N/A/**
691N/A * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
691N/A * Corresponds to setExplicitlyHidden.
691N/A */
691N/Abool SPItem::isExplicitlyHidden() const
691N/A{
691N/A return (style->display.set
691N/A && style->display.value == SP_CSS_DISPLAY_NONE);
691N/A}
691N/A
691N/A/**
691N/A * Sets the display CSS property to `hidden' if \a val is true,
691N/A * otherwise makes it unset
691N/A */
691N/Avoid SPItem::setExplicitlyHidden(bool val) {
691N/A style->display.set = val;
691N/A style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
691N/A style->display.computed = style->display.value;
691N/A updateRepr();
691N/A}
691N/A
691N/A/**
691N/A * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
691N/A */
691N/Avoid SPItem::setCenter(Geom::Point const &object_centre) {
691N/A document->ensureUpToDate();
691N/A
691N/A // FIXME this is seriously wrong
691N/A Geom::OptRect bbox = desktopGeometricBounds();
691N/A if (bbox) {
691N/A transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
691N/A if (Geom::are_near(transform_center_x, 0)) // rounding error
691N/A transform_center_x = 0;
691N/A transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
691N/A if (Geom::are_near(transform_center_y, 0)) // rounding error
691N/A transform_center_y = 0;
691N/A }
691N/A}
691N/A
691N/Avoid
691N/ASPItem::unsetCenter() {
691N/A transform_center_x = 0;
691N/A transform_center_y = 0;
691N/A}
691N/A
691N/Abool SPItem::isCenterSet() {
691N/A return (transform_center_x != 0 || transform_center_y != 0);
691N/A}
691N/A
691N/AGeom::Point SPItem::getCenter() const {
691N/A document->ensureUpToDate();
691N/A
691N/A // FIXME this is seriously wrong
691N/A Geom::OptRect bbox = desktopGeometricBounds();
691N/A if (bbox) {
691N/A return bbox->midpoint() + Geom::Point (transform_center_x, transform_center_y);
691N/A } else {
691N/A return Geom::Point(0, 0); // something's wrong!
691N/A }
691N/A}
691N/A
691N/A
691N/Anamespace {
691N/A
691N/Abool is_item(SPObject const &object) {
691N/A return SP_IS_ITEM(&object);
691N/A}
691N/A
691N/A}
691N/A
691N/Avoid SPItem::raiseToTop() {
691N/A using Inkscape::Algorithms::find_last_if;
691N/A
691N/A SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
691N/A next, NULL, &is_item
691N/A );
691N/A if (topmost) {
691N/A getRepr()->parent()->changeOrder( getRepr(), topmost->getRepr() );
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::raiseOne() {
691N/A SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
691N/A next, NULL, &is_item
691N/A );
691N/A if (next_higher) {
691N/A Inkscape::XML::Node *ref = next_higher->getRepr();
691N/A getRepr()->parent()->changeOrder(getRepr(), ref);
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::lowerOne() {
691N/A using Inkscape::Util::MutableList;
691N/A using Inkscape::Util::reverse_list;
691N/A
691N/A MutableList<SPObject &> next_lower=std::find_if(
691N/A reverse_list<SPObject::SiblingIterator>(
691N/A parent->firstChild(), this
691N/A ),
691N/A MutableList<SPObject &>(),
691N/A &is_item
691N/A );
691N/A if (next_lower) {
691N/A ++next_lower;
691N/A Inkscape::XML::Node *ref = ( next_lower ? next_lower->getRepr() : NULL );
691N/A getRepr()->parent()->changeOrder(getRepr(), ref);
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::lowerToBottom() {
691N/A using Inkscape::Algorithms::find_last_if;
691N/A using Inkscape::Util::MutableList;
691N/A using Inkscape::Util::reverse_list;
691N/A
691N/A MutableList<SPObject &> bottom=find_last_if(
691N/A reverse_list<SPObject::SiblingIterator>(
691N/A parent->firstChild(), this
691N/A ),
691N/A MutableList<SPObject &>(),
691N/A &is_item
691N/A );
691N/A if (bottom) {
691N/A ++bottom;
691N/A Inkscape::XML::Node *ref = ( bottom ? bottom->getRepr() : NULL );
691N/A getRepr()->parent()->changeOrder(getRepr(), ref);
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
691N/A{
691N/A object->readAttr( "style" );
691N/A object->readAttr( "transform" );
691N/A object->readAttr( "clip-path" );
691N/A object->readAttr( "mask" );
691N/A object->readAttr( "sodipodi:insensitive" );
691N/A object->readAttr( "sodipodi:nonprintable" );
691N/A object->readAttr( "inkscape:transform-center-x" );
691N/A object->readAttr( "inkscape:transform-center-y" );
691N/A object->readAttr( "inkscape:connector-avoid" );
691N/A object->readAttr( "inkscape:connection-points" );
691N/A
691N/A if (((SPObjectClass *) (SPItemClass::static_parent_class))->build) {
691N/A (* ((SPObjectClass *) (SPItemClass::static_parent_class))->build)(object, document, repr);
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::sp_item_release(SPObject *object)
691N/A{
691N/A SPItem *item = (SPItem *) object;
691N/A
691N/A // Note: do this here before the clip_ref is deleted, since calling
691N/A // ensureUpToDate() for triggered routing may reference
691N/A // the deleted clip_ref.
691N/A delete item->avoidRef;
691N/A
691N/A // we do NOT disconnect from the changed signal of those before deletion.
691N/A // The destructor will call *_ref_changed with NULL as the new value,
691N/A // which will cause the hide() function to be called.
691N/A delete item->clip_ref;
691N/A delete item->mask_ref;
691N/A
691N/A if (((SPObjectClass *) (SPItemClass::static_parent_class))->release) {
691N/A ((SPObjectClass *) SPItemClass::static_parent_class)->release(object);
691N/A }
691N/A
691N/A while (item->display) {
691N/A item->display = sp_item_view_list_remove(item->display, item->display);
691N/A }
691N/A
691N/A item->_transformed_signal.~signal();
691N/A}
691N/A
691N/Avoid SPItem::sp_item_set(SPObject *object, unsigned key, gchar const *value)
691N/A{
691N/A SPItem *item = (SPItem *) object;
691N/A
691N/A switch (key) {
691N/A case SP_ATTR_TRANSFORM: {
691N/A Geom::Affine t;
691N/A if (value && sp_svg_transform_read(value, &t)) {
691N/A item->set_item_transform(t);
691N/A } else {
691N/A item->set_item_transform(Geom::identity());
691N/A }
691N/A break;
691N/A }
691N/A case SP_PROP_CLIP_PATH: {
691N/A gchar *uri = extract_uri(value);
691N/A if (uri) {
691N/A try {
691N/A item->clip_ref->attach(Inkscape::URI(uri));
691N/A } catch (Inkscape::BadURIException &e) {
691N/A g_warning("%s", e.what());
691N/A item->clip_ref->detach();
691N/A }
691N/A g_free(uri);
691N/A } else {
691N/A item->clip_ref->detach();
691N/A }
691N/A
691N/A break;
691N/A }
691N/A case SP_PROP_MASK: {
691N/A gchar *uri = extract_uri(value);
691N/A if (uri) {
691N/A try {
691N/A item->mask_ref->attach(Inkscape::URI(uri));
691N/A } catch (Inkscape::BadURIException &e) {
691N/A g_warning("%s", e.what());
691N/A item->mask_ref->detach();
691N/A }
691N/A g_free(uri);
691N/A } else {
691N/A item->mask_ref->detach();
691N/A }
691N/A
691N/A break;
691N/A }
691N/A case SP_ATTR_SODIPODI_INSENSITIVE:
691N/A item->sensitive = !value;
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A v->arenaitem->setSensitive(item->sensitive);
691N/A }
691N/A break;
691N/A case SP_ATTR_CONNECTOR_AVOID:
691N/A item->avoidRef->setAvoid(value);
691N/A break;
691N/A case SP_ATTR_CONNECTION_POINTS:
691N/A item->avoidRef->setConnectionPoints(value);
691N/A break;
691N/A case SP_ATTR_TRANSFORM_CENTER_X:
691N/A if (value) {
691N/A item->transform_center_x = g_strtod(value, NULL);
691N/A } else {
691N/A item->transform_center_x = 0;
691N/A }
691N/A object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
691N/A break;
691N/A case SP_ATTR_TRANSFORM_CENTER_Y:
691N/A if (value) {
691N/A item->transform_center_y = g_strtod(value, NULL);
691N/A } else {
691N/A item->transform_center_y = 0;
691N/A }
691N/A object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
691N/A break;
691N/A case SP_PROP_SYSTEM_LANGUAGE:
691N/A case SP_PROP_REQUIRED_FEATURES:
691N/A case SP_PROP_REQUIRED_EXTENSIONS:
691N/A {
691N/A item->resetEvaluated();
691N/A // pass to default handler
691N/A }
691N/A default:
691N/A if (SP_ATTRIBUTE_IS_CSS(key)) {
691N/A sp_style_read_from_object(object->style, object);
691N/A object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
691N/A } else {
691N/A if (((SPObjectClass *) (SPItemClass::static_parent_class))->set) {
691N/A (* ((SPObjectClass *) (SPItemClass::static_parent_class))->set)(object, key, value);
691N/A }
691N/A }
691N/A break;
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
691N/A{
691N/A if (old_clip) {
691N/A SPItemView *v;
691N/A /* Hide clippath */
691N/A for (v = item->display; v != NULL; v = v->next) {
691N/A SP_CLIPPATH(old_clip)->hide(v->arenaitem->key());
691N/A }
691N/A }
691N/A if (SP_IS_CLIPPATH(clip)) {
691N/A Geom::OptRect bbox = item->geometricBounds();
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A if (!v->arenaitem->key()) {
691N/A v->arenaitem->setKey(SPItem::display_key_new(3));
691N/A }
691N/A Inkscape::DrawingItem *ai = SP_CLIPPATH(clip)->show(
691N/A v->arenaitem->drawing(),
691N/A v->arenaitem->key());
691N/A v->arenaitem->setClip(ai);
691N/A SP_CLIPPATH(clip)->setBBox(v->arenaitem->key(), bbox);
691N/A clip->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
691N/A }
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
691N/A{
691N/A if (old_mask) {
691N/A /* Hide mask */
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A sp_mask_hide(SP_MASK(old_mask), v->arenaitem->key());
691N/A }
691N/A }
691N/A if (SP_IS_MASK(mask)) {
691N/A Geom::OptRect bbox = item->geometricBounds();
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A if (!v->arenaitem->key()) {
691N/A v->arenaitem->setKey(SPItem::display_key_new(3));
691N/A }
691N/A Inkscape::DrawingItem *ai = sp_mask_show(SP_MASK(mask),
691N/A v->arenaitem->drawing(),
691N/A v->arenaitem->key());
691N/A v->arenaitem->setMask(ai);
691N/A sp_mask_set_bbox(SP_MASK(mask), v->arenaitem->key(), bbox);
691N/A mask->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
691N/A }
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
691N/A{
691N/A SPItem *item = SP_ITEM(object);
691N/A
691N/A if (((SPObjectClass *) (SPItemClass::static_parent_class))->update) {
691N/A (* ((SPObjectClass *) (SPItemClass::static_parent_class))->update)(object, ctx, flags);
691N/A }
691N/A
691N/A // any of the modifications defined in sp-object.h might change bbox,
691N/A // so we invalidate it unconditionally
691N/A item->bbox_valid = FALSE;
691N/A
691N/A if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
691N/A if (flags & SP_OBJECT_MODIFIED_FLAG) {
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A v->arenaitem->setTransform(item->transform);
691N/A }
691N/A }
691N/A
691N/A SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
691N/A SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
691N/A
691N/A if ( clip_path || mask ) {
691N/A Geom::OptRect bbox = item->geometricBounds();
691N/A if (clip_path) {
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A clip_path->setBBox(v->arenaitem->key(), bbox);
691N/A }
691N/A }
691N/A if (mask) {
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A sp_mask_set_bbox(mask, v->arenaitem->key(), bbox);
691N/A }
691N/A }
691N/A }
691N/A
691N/A if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
691N/A for (SPItemView *v = item->display; v != NULL; v = v->next) {
691N/A v->arenaitem->setOpacity(SP_SCALE24_TO_FLOAT(object->style->opacity.value));
691N/A v->arenaitem->setVisible(!item->isHidden());
691N/A }
691N/A }
691N/A }
691N/A
691N/A /* Update bounding box data used by filters */
691N/A if (item->style->filter.set && item->display) {
691N/A Geom::OptRect item_bbox = item->geometricBounds();
691N/A
691N/A SPItemView *itemview = item->display;
691N/A do {
691N/A if (itemview->arenaitem)
691N/A itemview->arenaitem->setItemBounds(item_bbox);
691N/A } while ( (itemview = itemview->next) );
691N/A }
691N/A
691N/A // Update libavoid with item geometry (for connector routing).
691N/A if (item->avoidRef)
691N/A item->avoidRef->handleSettingChange();
691N/A}
691N/A
691N/AInkscape::XML::Node *SPItem::sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
691N/A{
691N/A SPItem *item = SP_ITEM(object);
691N/A
691N/A // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
691N/A // so we need to add any children from the underlying object to the new repr
691N/A if (flags & SP_OBJECT_WRITE_BUILD) {
691N/A GSList *l = NULL;
691N/A for (SPObject *child = object->firstChild(); child != NULL; child = child->next ) {
691N/A if (SP_IS_TITLE(child) || SP_IS_DESC(child)) {
691N/A Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
691N/A if (crepr) {
691N/A l = g_slist_prepend (l, crepr);
691N/A }
691N/A }
691N/A }
691N/A while (l) {
691N/A repr->addChild((Inkscape::XML::Node *) l->data, NULL);
691N/A Inkscape::GC::release((Inkscape::XML::Node *) l->data);
691N/A l = g_slist_remove (l, l->data);
691N/A }
691N/A } else {
691N/A for (SPObject *child = object->firstChild() ; child != NULL; child = child->next ) {
691N/A if (SP_IS_TITLE(child) || SP_IS_DESC(child)) {
691N/A child->updateRepr(flags);
691N/A }
691N/A }
691N/A }
691N/A
691N/A gchar *c = sp_svg_transform_write(item->transform);
691N/A repr->setAttribute("transform", c);
691N/A g_free(c);
691N/A
691N/A if (flags & SP_OBJECT_WRITE_EXT) {
691N/A repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
691N/A if (item->transform_center_x != 0)
691N/A sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
691N/A else
691N/A repr->setAttribute ("inkscape:transform-center-x", NULL);
691N/A if (item->transform_center_y != 0)
691N/A sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
691N/A else
691N/A repr->setAttribute ("inkscape:transform-center-y", NULL);
691N/A }
691N/A
691N/A if (item->clip_ref->getObject()) {
691N/A const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
691N/A repr->setAttribute ("clip-path", value);
691N/A g_free ((void *) value);
691N/A }
691N/A if (item->mask_ref->getObject()) {
691N/A const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
691N/A repr->setAttribute ("mask", value);
691N/A g_free ((void *) value);
691N/A }
691N/A
691N/A if (((SPObjectClass *) (SPItemClass::static_parent_class))->write) {
691N/A ((SPObjectClass *) (SPItemClass::static_parent_class))->write(object, xml_doc, repr, flags);
691N/A }
691N/A
691N/A return repr;
691N/A}
691N/A
691N/A/** @brief Get item's geometric bounding box in this item's coordinate system.
691N/A * The geometric bounding box includes only the path, disregarding all style attributes. */
691N/AGeom::OptRect SPItem::geometricBounds(Geom::Affine const &transform) const
691N/A{
691N/A Geom::OptRect bbox;
691N/A // call the subclass method
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox) {
691N/A bbox = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox(this, transform, SPItem::GEOMETRIC_BBOX);
691N/A }
691N/A return bbox;
691N/A}
691N/A
691N/A/** @brief Get item's visual bounding box in this item's coordinate system.
691N/A * The visual bounding box includes the stroke and the filter region. */
691N/AGeom::OptRect SPItem::visualBounds(Geom::Affine const &transform) const
691N/A{
691N/A using Geom::X;
691N/A using Geom::Y;
691N/A
691N/A Geom::OptRect bbox;
691N/A
691N/A if ( style && style->filter.href && style->getFilter() && SP_IS_FILTER(style->getFilter())) {
691N/A // call the subclass method
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox) {
691N/A bbox = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox(this, Geom::identity(), SPItem::VISUAL_BBOX);
691N/A }
691N/A
691N/A SPFilter *filter = SP_FILTER(style->getFilter());
691N/A // default filer area per the SVG spec:
691N/A SVGLength x, y, w, h;
691N/A Geom::Point minp, maxp;
691N/A x.set(SVGLength::PERCENT, -0.10, 0);
691N/A y.set(SVGLength::PERCENT, -0.10, 0);
691N/A w.set(SVGLength::PERCENT, 1.20, 0);
691N/A h.set(SVGLength::PERCENT, 1.20, 0);
691N/A
691N/A // if area is explicitly set, override:
691N/A if (filter->x._set)
691N/A x = filter->x;
691N/A if (filter->y._set)
691N/A y = filter->y;
691N/A if (filter->width._set)
691N/A w = filter->width;
691N/A if (filter->height._set)
691N/A h = filter->height;
691N/A
691N/A double len_x = bbox ? bbox->width() : 0;
691N/A double len_y = bbox ? bbox->height() : 0;
691N/A
691N/A x.update(12, 6, len_x);
691N/A y.update(12, 6, len_y);
691N/A w.update(12, 6, len_x);
691N/A h.update(12, 6, len_y);
691N/A
691N/A if (filter->filterUnits == SP_FILTER_UNITS_OBJECTBOUNDINGBOX && bbox) {
691N/A minp[X] = bbox->left() + x.computed * (x.unit == SVGLength::PERCENT ? 1.0 : len_x);
691N/A maxp[X] = minp[X] + w.computed * (w.unit == SVGLength::PERCENT ? 1.0 : len_x);
691N/A minp[Y] = bbox->top() + y.computed * (y.unit == SVGLength::PERCENT ? 1.0 : len_y);
691N/A maxp[Y] = minp[Y] + h.computed * (h.unit == SVGLength::PERCENT ? 1.0 : len_y);
691N/A } else if (filter->filterUnits == SP_FILTER_UNITS_USERSPACEONUSE) {
691N/A minp[X] = x.computed;
691N/A maxp[X] = minp[X] + w.computed;
691N/A minp[Y] = y.computed;
691N/A maxp[Y] = minp[Y] + h.computed;
691N/A }
691N/A bbox = Geom::OptRect(minp, maxp);
691N/A *bbox *= transform;
691N/A } else {
691N/A // call the subclass method
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox) {
691N/A bbox = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox(this, transform, SPItem::VISUAL_BBOX);
691N/A }
691N/A }
691N/A if (clip_ref->getObject()) {
691N/A bbox.intersectWith(SP_CLIPPATH(clip_ref->getObject())->geometricBounds(transform));
691N/A }
691N/A
691N/A return bbox;
691N/A}
691N/AGeom::OptRect SPItem::bounds(BBoxType type, Geom::Affine const &transform) const
691N/A{
691N/A if (type == GEOMETRIC_BBOX) {
691N/A return geometricBounds(transform);
691N/A } else {
691N/A return visualBounds(transform);
691N/A }
691N/A}
691N/A
691N/A/** Get item's geometric bbox in document coordinate system.
691N/A * Document coordinates are the default coordinates of the root element:
691N/A * the origin is at the top left, X grows to the right and Y grows downwards. */
691N/AGeom::OptRect SPItem::documentGeometricBounds() const
691N/A{
691N/A return geometricBounds(i2doc_affine());
691N/A}
691N/A/// Get item's visual bbox in document coordinate system.
691N/AGeom::OptRect SPItem::documentVisualBounds() const
691N/A{
691N/A if (!bbox_valid) {
691N/A doc_bbox = visualBounds(i2doc_affine());
691N/A bbox_valid = true;
691N/A }
691N/A return doc_bbox;
691N/A}
691N/AGeom::OptRect SPItem::documentBounds(BBoxType type) const
691N/A{
691N/A if (type == GEOMETRIC_BBOX) {
691N/A return documentGeometricBounds();
691N/A } else {
691N/A return documentVisualBounds();
691N/A }
691N/A}
691N/A/** Get item's geometric bbox in desktop coordinate system.
691N/A * Desktop coordinates should be user defined. Currently they are hardcoded:
691N/A * origin is at bottom left, X grows to the right and Y grows upwards. */
691N/AGeom::OptRect SPItem::desktopGeometricBounds() const
691N/A{
691N/A return geometricBounds(i2dt_affine());
691N/A}
691N/A/// Get item's visual bbox in desktop coordinate system.
691N/AGeom::OptRect SPItem::desktopVisualBounds() const
691N/A{
691N/A /// @fixme hardcoded desktop transform
691N/A Geom::Affine m = Geom::Scale(1, -1) * Geom::Translate(0, document->getHeight());
691N/A Geom::OptRect ret = documentVisualBounds();
691N/A if (ret) *ret *= m;
691N/A return ret;
691N/A}
691N/A
691N/AGeom::OptRect SPItem::desktopPreferredBounds() const
691N/A{
691N/A if (Inkscape::Preferences::get()->getInt("/tools/bounding_box") == 0) {
691N/A return desktopBounds(SPItem::VISUAL_BBOX);
691N/A } else {
691N/A return desktopBounds(SPItem::GEOMETRIC_BBOX);
691N/A }
691N/A}
691N/A
691N/AGeom::OptRect SPItem::desktopBounds(BBoxType type) const
691N/A{
691N/A if (type == GEOMETRIC_BBOX) {
691N/A return desktopGeometricBounds();
691N/A } else {
691N/A return desktopVisualBounds();
691N/A }
691N/A}
691N/A
691N/Aunsigned SPItem::pos_in_parent()
691N/A{
691N/A g_assert(parent != NULL);
691N/A g_assert(SP_IS_OBJECT(parent));
691N/A
691N/A SPObject *object = this;
691N/A
691N/A unsigned pos=0;
691N/A for ( SPObject *iter = parent->firstChild() ; iter ; iter = iter->next) {
691N/A if ( iter == object ) {
691N/A return pos;
691N/A }
691N/A if (SP_IS_ITEM(iter)) {
691N/A pos++;
691N/A }
691N/A }
691N/A
691N/A g_assert_not_reached();
691N/A return 0;
691N/A}
691N/A
691N/Avoid SPItem::sp_item_private_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
691N/A{
691N/A /* This will only be called if the derived class doesn't override this.
691N/A * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
691N/A * We don't know what shape we could be dealing with here, so we'll just
691N/A * do nothing
691N/A */
691N/A}
691N/A
691N/A
691N/Avoid SPItem::getSnappoints(std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs) const
691N/A{
691N/A // Get the snappoints of the item
691N/A SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(this);
691N/A if (item_class.snappoints) {
691N/A item_class.snappoints(this, p, snapprefs);
691N/A }
691N/A
691N/A // Get the snappoints at the item's center
691N/A if (snapprefs != NULL && snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_ROTATION_CENTER)) {
691N/A p.push_back(Inkscape::SnapCandidatePoint(getCenter(), Inkscape::SNAPSOURCE_ROTATION_CENTER, Inkscape::SNAPTARGET_ROTATION_CENTER));
691N/A }
691N/A
691N/A // Get the snappoints of clipping paths and mask, if any
691N/A std::list<SPObject const *> clips_and_masks;
691N/A
691N/A clips_and_masks.push_back(clip_ref->getObject());
691N/A clips_and_masks.push_back(mask_ref->getObject());
691N/A
691N/A SPDesktop *desktop = inkscape_active_desktop();
691N/A for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
691N/A if (*o) {
691N/A // obj is a group object, the children are the actual clippers
691N/A for (SPObject *child = (*o)->children ; child ; child = child->next) {
691N/A if (SP_IS_ITEM(child)) {
691N/A std::vector<Inkscape::SnapCandidatePoint> p_clip_or_mask;
691N/A // Please note the recursive call here!
691N/A SP_ITEM(child)->getSnappoints(p_clip_or_mask, snapprefs);
691N/A // Take into account the transformation of the item being clipped or masked
691N/A for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
691N/A // All snappoints are in desktop coordinates, but the item's transformation is
691N/A // in document coordinates. Hence the awkward construction below
691N/A Geom::Point pt = desktop->dt2doc((*p_orig).getPoint()) * i2dt_affine();
691N/A p.push_back(Inkscape::SnapCandidatePoint(pt, (*p_orig).getSourceType(), (*p_orig).getTargetType()));
691N/A }
691N/A }
691N/A }
691N/A }
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::invoke_print(SPPrintContext *ctx)
691N/A{
691N/A if ( !isHidden() ) {
691N/A if ( reinterpret_cast<SPItemClass *>(G_OBJECT_GET_CLASS(this))->print ) {
691N/A if (!transform.isIdentity()
691N/A || style->opacity.value != SP_SCALE24_MAX)
691N/A {
691N/A sp_print_bind(ctx, transform, SP_SCALE24_TO_FLOAT(style->opacity.value));
691N/A reinterpret_cast<SPItemClass *>(G_OBJECT_GET_CLASS(this))->print(this, ctx);
691N/A sp_print_release(ctx);
691N/A } else {
691N/A reinterpret_cast<SPItemClass *>(G_OBJECT_GET_CLASS(this))->print(this, ctx);
691N/A }
691N/A }
691N/A }
691N/A}
691N/A
691N/Agchar *SPItem::sp_item_private_description(SPItem */*item*/)
691N/A{
691N/A return g_strdup(_("Object"));
691N/A}
691N/A
691N/A/**
691N/A * Returns a string suitable for status bar, formatted in pango markup language.
691N/A *
691N/A * Must be freed by caller.
691N/A */
691N/Agchar *SPItem::description()
691N/A{
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->description) {
691N/A gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->description(this);
691N/A if (s && clip_ref->getObject()) {
691N/A gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
691N/A g_free (s);
691N/A s = snew;
691N/A }
691N/A if (s && mask_ref->getObject()) {
691N/A gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
691N/A g_free (s);
691N/A s = snew;
691N/A }
691N/A if ( style && style->filter.href && style->filter.href->getObject() ) {
691N/A const gchar *label = style->filter.href->getObject()->label();
691N/A gchar *snew = 0;
691N/A if (label) {
691N/A snew = g_strdup_printf (_("%s; <i>filtered (%s)</i>"), s, _(label));
691N/A } else {
691N/A snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
691N/A }
691N/A g_free (s);
691N/A s = snew;
691N/A }
691N/A return s;
691N/A }
691N/A
691N/A g_assert_not_reached();
691N/A return NULL;
691N/A}
691N/A
691N/A/**
691N/A * Allocates unique integer keys.
691N/A * \param numkeys Number of keys required.
691N/A * \return First allocated key; hence if the returned key is n
691N/A * you can use n, n + 1, ..., n + (numkeys - 1)
691N/A */
691N/Aunsigned SPItem::display_key_new(unsigned numkeys)
691N/A{
691N/A static unsigned dkey = 0;
691N/A
691N/A dkey += numkeys;
691N/A
691N/A return dkey - numkeys;
691N/A}
691N/A
691N/AInkscape::DrawingItem *SPItem::invoke_show(Inkscape::Drawing &drawing, unsigned key, unsigned flags)
691N/A{
691N/A Inkscape::DrawingItem *ai = NULL;
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->show) {
691N/A ai = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->show(this, drawing, key, flags);
691N/A }
691N/A
691N/A if (ai != NULL) {
691N/A Geom::OptRect item_bbox = geometricBounds();
691N/A
691N/A display = sp_item_view_new_prepend(display, this, flags, key, ai);
691N/A ai->setTransform(transform);
691N/A ai->setOpacity(SP_SCALE24_TO_FLOAT(style->opacity.value));
691N/A ai->setVisible(!isHidden());
691N/A ai->setSensitive(sensitive);
691N/A if (clip_ref->getObject()) {
691N/A SPClipPath *cp = clip_ref->getObject();
691N/A
691N/A if (!display->arenaitem->key()) {
691N/A display->arenaitem->setKey(display_key_new(3));
691N/A }
691N/A int clip_key = display->arenaitem->key();
691N/A
691N/A // Show and set clip
691N/A Inkscape::DrawingItem *ac = cp->show(drawing, clip_key);
691N/A ai->setClip(ac);
691N/A
691N/A // Update bbox, in case the clip uses bbox units
691N/A SP_CLIPPATH(cp)->setBBox(clip_key, item_bbox);
691N/A cp->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
691N/A }
691N/A if (mask_ref->getObject()) {
691N/A SPMask *mask = mask_ref->getObject();
691N/A
691N/A if (!display->arenaitem->key()) {
691N/A display->arenaitem->setKey(display_key_new(3));
691N/A }
691N/A int mask_key = display->arenaitem->key();
691N/A
691N/A // Show and set mask
691N/A Inkscape::DrawingItem *ac = sp_mask_show(mask, drawing, mask_key);
691N/A ai->setMask(ac);
691N/A
691N/A // Update bbox, in case the mask uses bbox units
691N/A sp_mask_set_bbox(SP_MASK(mask), mask_key, item_bbox);
691N/A mask->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
691N/A }
691N/A ai->setData(this);
691N/A ai->setItemBounds(item_bbox);
691N/A }
691N/A
691N/A return ai;
691N/A}
691N/A
691N/Avoid SPItem::invoke_hide(unsigned key)
691N/A{
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->hide) {
691N/A ((SPItemClass *) G_OBJECT_GET_CLASS(this))->hide(this, key);
691N/A }
691N/A
691N/A SPItemView *ref = NULL;
691N/A SPItemView *v = display;
691N/A while (v != NULL) {
691N/A SPItemView *next = v->next;
691N/A if (v->key == key) {
691N/A if (clip_ref->getObject()) {
691N/A (clip_ref->getObject())->hide(v->arenaitem->key());
691N/A v->arenaitem->setClip(NULL);
691N/A }
691N/A if (mask_ref->getObject()) {
691N/A sp_mask_hide(mask_ref->getObject(), v->arenaitem->key());
691N/A v->arenaitem->setMask(NULL);
691N/A }
691N/A if (!ref) {
691N/A display = v->next;
691N/A } else {
691N/A ref->next = v->next;
691N/A }
691N/A delete v->arenaitem;
691N/A g_free(v);
691N/A } else {
691N/A ref = v;
691N/A }
691N/A v = next;
691N/A }
691N/A}
691N/A
691N/A// Adjusters
691N/A
691N/Avoid SPItem::adjust_pattern (Geom::Affine const &postmul, bool set)
691N/A{
691N/A if (style && (style->fill.isPaintserver())) {
691N/A SPObject *server = style->getFillPaintServer();
691N/A if ( SP_IS_PATTERN(server) ) {
691N/A SPPattern *pattern = sp_pattern_clone_if_necessary(this, SP_PATTERN(server), "fill");
691N/A sp_pattern_transform_multiply(pattern, postmul, set);
691N/A }
691N/A }
691N/A
691N/A if (style && (style->stroke.isPaintserver())) {
691N/A SPObject *server = style->getStrokePaintServer();
691N/A if ( SP_IS_PATTERN(server) ) {
691N/A SPPattern *pattern = sp_pattern_clone_if_necessary(this, SP_PATTERN(server), "stroke");
691N/A sp_pattern_transform_multiply(pattern, postmul, set);
691N/A }
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::adjust_gradient( Geom::Affine const &postmul, bool set )
691N/A{
691N/A if ( style && style->fill.isPaintserver() ) {
691N/A SPPaintServer *server = style->getFillPaintServer();
691N/A if ( SP_IS_GRADIENT(server) ) {
691N/A
691N/A /**
691N/A * \note Bbox units for a gradient are generally a bad idea because
691N/A * with them, you cannot preserve the relative position of the
691N/A * object and its gradient after rotation or skew. So now we
691N/A * convert them to userspace units which are easy to keep in sync
691N/A * just by adding the object's transform to gradientTransform.
691N/A * \todo FIXME: convert back to bbox units after transforming with
691N/A * the item, so as to preserve the original units.
691N/A */
691N/A SPGradient *gradient = sp_gradient_convert_to_userspace( SP_GRADIENT(server), this, "fill" );
691N/A
691N/A sp_gradient_transform_multiply( gradient, postmul, set );
691N/A }
691N/A }
691N/A
691N/A if ( style && style->stroke.isPaintserver() ) {
691N/A SPPaintServer *server = style->getStrokePaintServer();
691N/A if ( SP_IS_GRADIENT(server) ) {
691N/A SPGradient *gradient = sp_gradient_convert_to_userspace( SP_GRADIENT(server), this, "stroke");
691N/A sp_gradient_transform_multiply( gradient, postmul, set );
691N/A }
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::adjust_stroke( gdouble ex )
691N/A{
691N/A if (freeze_stroke_width) {
691N/A return;
691N/A }
691N/A
691N/A SPStyle *style = this->style;
691N/A
691N/A if (style && !style->stroke.isNone() && !Geom::are_near(ex, 1.0, Geom::EPSILON)) {
691N/A style->stroke_width.computed *= ex;
691N/A style->stroke_width.set = TRUE;
691N/A
691N/A if ( style->stroke_dash.n_dash != 0 ) {
691N/A for (int i = 0; i < style->stroke_dash.n_dash; i++) {
691N/A style->stroke_dash.dash[i] *= ex;
691N/A }
691N/A style->stroke_dash.offset *= ex;
691N/A }
691N/A
691N/A updateRepr();
691N/A }
691N/A}
691N/A
691N/A/**
691N/A * Find out the inverse of previous transform of an item (from its repr)
691N/A */
691N/AGeom::Affine sp_item_transform_repr (SPItem *item)
691N/A{
691N/A Geom::Affine t_old(Geom::identity());
691N/A gchar const *t_attr = item->getRepr()->attribute("transform");
691N/A if (t_attr) {
691N/A Geom::Affine t;
691N/A if (sp_svg_transform_read(t_attr, &t)) {
691N/A t_old = t;
691N/A }
691N/A }
691N/A
691N/A return t_old;
691N/A}
691N/A
691N/A
691N/A/**
691N/A * Recursively scale stroke width in \a item and its children by \a expansion.
691N/A */
691N/Avoid SPItem::adjust_stroke_width_recursive(double expansion)
691N/A{
691N/A adjust_stroke (expansion);
691N/A
691N/A// A clone's child is the ghost of its original - we must not touch it, skip recursion
691N/A if ( !SP_IS_USE(this) ) {
691N/A for ( SPObject *o = children; o; o = o->getNext() ) {
691N/A if (SP_IS_ITEM(o)) {
691N/A SP_ITEM(o)->adjust_stroke_width_recursive(expansion);
691N/A }
691N/A }
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::freeze_stroke_width_recursive(bool freeze)
691N/A{
691N/A freeze_stroke_width = freeze;
691N/A
691N/A// A clone's child is the ghost of its original - we must not touch it, skip recursion
691N/A if ( !SP_IS_USE(this) ) {
691N/A for ( SPObject *o = children; o; o = o->getNext() ) {
691N/A if (SP_IS_ITEM(o)) {
691N/A SP_ITEM(o)->freeze_stroke_width_recursive(freeze);
691N/A }
691N/A }
691N/A }
691N/A}
691N/A
691N/A/**
691N/A * Recursively adjust rx and ry of rects.
691N/A */
691N/Avoid
691N/Asp_item_adjust_rects_recursive(SPItem *item, Geom::Affine advertized_transform)
691N/A{
691N/A if (SP_IS_RECT (item)) {
691N/A sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
691N/A }
691N/A
691N/A for (SPObject *o = item->children; o != NULL; o = o->next) {
691N/A if (SP_IS_ITEM(o))
691N/A sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
691N/A }
691N/A}
691N/A
691N/A/**
691N/A * Recursively compensate pattern or gradient transform.
691N/A */
691N/Avoid SPItem::adjust_paint_recursive (Geom::Affine advertized_transform, Geom::Affine t_ancestors, bool is_pattern)
691N/A{
691N/A// _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
691N/A// _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
691N/A// By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
691N/A Geom::Affine t_item = sp_item_transform_repr (this);
691N/A Geom::Affine paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
691N/A
691N/A// Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
691N/A// also we do not recurse into clones, because a clone's child is the ghost of its original -
691N/A// we must not touch it
691N/A if (!(this && (SP_IS_TEXT(this) || SP_IS_USE(this)))) {
691N/A for (SPObject *o = children; o != NULL; o = o->next) {
691N/A if (SP_IS_ITEM(o)) {
691N/A// At the level of the transformed item, t_ancestors is identity;
691N/A// below it, it is the accmmulated chain of transforms from this level to the top level
691N/A SP_ITEM(o)->adjust_paint_recursive (advertized_transform, t_item * t_ancestors, is_pattern);
691N/A }
691N/A }
691N/A }
691N/A
691N/A// We recursed into children first, and are now adjusting this object second;
691N/A// this is so that adjustments in a tree are done from leaves up to the root,
691N/A// and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
691N/A// before ancestors themselves are adjusted, probably differently (bug 1286535)
691N/A
691N/A if (is_pattern) {
691N/A adjust_pattern(paint_delta);
691N/A } else {
691N/A adjust_gradient(paint_delta);
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::adjust_livepatheffect (Geom::Affine const &postmul, bool set)
691N/A{
691N/A if ( SP_IS_LPE_ITEM(this) ) {
691N/A SPLPEItem *lpeitem = SP_LPE_ITEM (this);
691N/A if ( sp_lpe_item_has_path_effect(lpeitem) ) {
691N/A sp_lpe_item_fork_path_effects_if_necessary(lpeitem);
691N/A
691N/A // now that all LPEs are forked_if_necessary, we can apply the transform
691N/A PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
691N/A for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
691N/A {
691N/A LivePathEffectObject *lpeobj = (*it)->lpeobject;
691N/A if (lpeobj && lpeobj->get_lpe()) {
691N/A Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
691N/A effect->transform_multiply(postmul, set);
691N/A }
691N/A }
691N/A }
691N/A }
691N/A}
691N/A
691N/A/**
691N/A * Set a new transform on an object.
691N/A *
691N/A * Compensate for stroke scaling and gradient/pattern fill transform, if
691N/A * necessary. Call the object's set_transform method if transforms are
691N/A * stored optimized. Send _transformed_signal. Invoke _write method so that
691N/A * the repr is updated with the new transform.
691N/A */
691N/Avoid SPItem::doWriteTransform(Inkscape::XML::Node *repr, Geom::Affine const &transform, Geom::Affine const *adv, bool compensate)
691N/A{
691N/A g_return_if_fail(repr != NULL);
691N/A
691N/A // calculate the relative transform, if not given by the adv attribute
691N/A Geom::Affine advertized_transform;
691N/A if (adv != NULL) {
691N/A advertized_transform = *adv;
691N/A } else {
691N/A advertized_transform = sp_item_transform_repr (this).inverse() * transform;
691N/A }
691N/A
691N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
691N/A if (compensate) {
691N/A
691N/A // recursively compensating for stroke scaling will not work, because it can be scaled to zero or infinite
691N/A // from which we cannot ever recover by applying an inverse scale; therefore we temporarily block any changes
691N/A // to the strokewidth instead, and unblock these after the transformation
691N/A // (as reported in https://bugs.launchpad.net/inkscape/+bug/825840/comments/4)
691N/A if (!prefs->getBool("/options/transform/stroke", true)) {
691N/A freeze_stroke_width_recursive(true);
691N/A // This will only work if the item has a set_transform method (in this method adjust_stroke() will be called)
691N/A // We will still have to apply the inverse scaling to other items, not having a set_transform method
691N/A // such as ellipses and stars
691N/A }
691N/A
691N/A // recursively compensate rx/ry of a rect if requested
691N/A if (!prefs->getBool("/options/transform/rectcorners", true)) {
691N/A sp_item_adjust_rects_recursive(this, advertized_transform);
691N/A }
691N/A
691N/A // recursively compensate pattern fill if it's not to be transformed
691N/A if (!prefs->getBool("/options/transform/pattern", true)) {
691N/A adjust_paint_recursive (advertized_transform.inverse(), Geom::identity(), true);
691N/A }
691N/A /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
691N/A /// recursively compensate gradient fill if it's not to be transformed
691N/A if (!prefs->getBool("/options/transform/gradient", true)) {
691N/A adjust_paint_recursive (advertized_transform.inverse(), Geom::identity(), false);
691N/A } else {
691N/A // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
691N/A // it here _before_ the new transform is set, so as to use the pre-transform bbox
691N/A adjust_paint_recursive (Geom::identity(), Geom::identity(), false);
691N/A }
691N/A
691N/A } // endif(compensate)
691N/A
691N/A gint preserve = prefs->getBool("/options/preservetransform/value", 0);
691N/A Geom::Affine transform_attr (transform);
691N/A if ( // run the object's set_transform (i.e. embed transform) only if:
691N/A ((SPItemClass *) G_OBJECT_GET_CLASS(this))->set_transform && // it does have a set_transform method
691N/A !preserve && // user did not chose to preserve all transforms
691N/A !clip_ref->getObject() && // the object does not have a clippath
691N/A !mask_ref->getObject() && // the object does not have a mask
691N/A !(!transform.isTranslation() && style && style->getFilter()) // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
691N/A ) {
691N/A transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->set_transform(this, transform);
691N/A freeze_stroke_width_recursive(false);
691N/A } else {
691N/A freeze_stroke_width_recursive(false);
691N/A if (compensate) {
691N/A if (!prefs->getBool("/options/transform/stroke", true)) {
691N/A // Recursively compensate for stroke scaling, depending on user preference
691N/A // (As to why we need to do this, see the comment a few lines above near the freeze_stroke_width_recursive(true) call)
691N/A double const expansion = 1. / advertized_transform.descrim();
691N/A adjust_stroke_width_recursive(expansion);
691N/A }
691N/A }
691N/A }
691N/A set_item_transform(transform_attr);
691N/A
691N/A
691N/A
691N/A // Note: updateRepr comes before emitting the transformed signal since
691N/A // it causes clone SPUse's copy of the original object to brought up to
691N/A // date with the original. Otherwise, sp_use_bbox returns incorrect
691N/A // values if called in code handling the transformed signal.
691N/A updateRepr();
691N/A
691N/A // send the relative transform with a _transformed_signal
691N/A _transformed_signal.emit(&advertized_transform, this);
691N/A}
691N/A
691N/Agint SPItem::emitEvent(SPEvent &event)
691N/A{
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->event) {
691N/A return ((SPItemClass *) G_OBJECT_GET_CLASS(this))->event(this, &event);
691N/A }
691N/A
691N/A return FALSE;
691N/A}
691N/A
691N/A/**
691N/A * Sets item private transform (not propagated to repr), without compensating stroke widths,
691N/A * gradients, patterns as sp_item_write_transform does.
691N/A */
691N/Avoid SPItem::set_item_transform(Geom::Affine const &transform_matrix)
691N/A{
691N/A if (!Geom::are_near(transform_matrix, transform, 1e-18)) {
691N/A transform = transform_matrix;
691N/A /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
691N/A transformation. It's apparently not used anywhere else. */
691N/A requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
691N/A sp_item_rm_unsatisfied_cns(*this);
691N/A }
691N/A}
691N/A
691N/Avoid SPItem::convert_item_to_guides() {
691N/A // Use derived method if present ...
691N/A if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->convert_to_guides) {
691N/A (*((SPItemClass *) G_OBJECT_GET_CLASS(this))->convert_to_guides)(this);
691N/A } else {
691N/A // .. otherwise simply place the guides around the item's bounding box
691N/A
691N/A convert_to_guides();
691N/A }
691N/A}
691N/A
691N/A
691N/A/**
691N/A * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
691N/A * ("Ancestor (\>=)" here includes as far as \a object itself.)
691N/A */
691N/AGeom::Affine
691N/Ai2anc_affine(SPObject const *object, SPObject const *const ancestor) {
691N/A Geom::Affine ret(Geom::identity());
691N/A g_return_val_if_fail(object != NULL, ret);
691N/A
691N/A /* stop at first non-renderable ancestor */
691N/A while ( object != ancestor && SP_IS_ITEM(object) ) {
691N/A if (SP_IS_ROOT(object)) {
691N/A ret *= SP_ROOT(object)->c2p;
691N/A } else {
691N/A ret *= SP_ITEM(object)->transform;
691N/A }
691N/A object = object->parent;
691N/A }
691N/A return ret;
691N/A}
691N/A
691N/AGeom::Affine
691N/Ai2i_affine(SPObject const *src, SPObject const *dest) {
691N/A g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
691N/A SPObject const *ancestor = src->nearestCommonAncestor(dest);
691N/A return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
691N/A}
691N/A
691N/AGeom::Affine SPItem::getRelativeTransform(SPObject const *dest) const {
691N/A return i2i_affine(this, dest);
691N/A}
691N/A
691N/A/**
691N/A * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
691N/A * \pre (item != NULL) and SP_IS_ITEM(item).
691N/A */
691N/AGeom::Affine SPItem::i2doc_affine() const
691N/A{
691N/A return i2anc_affine(this, NULL);
691N/A}
691N/A
691N/A/**
691N/A * Returns the transformation from item to desktop coords
691N/A */
691N/AGeom::Affine SPItem::i2dt_affine() const
691N/A{
691N/A Geom::Affine ret;
691N/A SPDesktop const *desktop = inkscape_active_desktop();
691N/A if ( desktop ) {
691N/A ret = i2doc_affine() * desktop->doc2dt();
691N/A } else {
691N/A // TODO temp code to prevent crashing on command-line launch:
691N/A ret = i2doc_affine()
691N/A * Geom::Scale(1, -1)
691N/A * Geom::Translate(0, document->getHeight());
691N/A
691N/A g_return_val_if_fail(desktop != NULL, ret);
691N/A }
691N/A return ret;
691N/A}
691N/A
691N/Avoid SPItem::set_i2d_affine(Geom::Affine const &i2dt)
691N/A{
691N/A Geom::Affine dt2p; /* desktop to item parent transform */
691N/A if (parent) {
691N/A dt2p = static_cast<SPItem *>(parent)->i2dt_affine().inverse();
691N/A } else {
691N/A SPDesktop *dt = inkscape_active_desktop();
691N/A dt2p = dt->dt2doc();
691N/A }
691N/A
691N/A Geom::Affine const i2p( i2dt * dt2p );
691N/A set_item_transform(i2p);
691N/A}
691N/A
691N/A
691N/A/**
691N/A * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
691N/A */
691N/AGeom::Affine SPItem::dt2i_affine() const
691N/A{
691N/A /* fixme: Implement the right way (Lauris) */
691N/A return i2dt_affine().inverse();
691N/A}
691N/A
691N/A/* Item views */
691N/A
691N/ASPItemView *SPItem::sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, Inkscape::DrawingItem *drawing_item)
691N/A{
691N/A g_assert(item != NULL);
691N/A g_assert(SP_IS_ITEM(item));
691N/A g_assert(drawing_item != NULL);
691N/A
691N/A SPItemView *new_view = g_new(SPItemView, 1);
691N/A
691N/A new_view->next = list;
691N/A new_view->flags = flags;
691N/A new_view->key = key;
691N/A new_view->arenaitem = drawing_item;
691N/A
691N/A return new_view;
691N/A}
691N/A
691N/ASPItemView *SPItem::sp_item_view_list_remove(SPItemView *list, SPItemView *view)
691N/A{
691N/A SPItemView *ret = list;
691N/A if (view == list) {
691N/A ret = list->next;
691N/A } else {
691N/A SPItemView *prev;
691N/A prev = list;
691N/A while (prev->next != view) prev = prev->next;
691N/A prev->next = view->next;
691N/A }
691N/A
691N/A delete view->arenaitem;
691N/A g_free(view);
691N/A
691N/A return ret;
691N/A}
691N/A
691N/A/**
691N/A * Return the arenaitem corresponding to the given item in the display
691N/A * with the given key
691N/A */
691N/AInkscape::DrawingItem *SPItem::get_arenaitem(unsigned key)
691N/A{
691N/A for ( SPItemView *iv = display ; iv ; iv = iv->next ) {
691N/A if ( iv->key == key ) {
691N/A return iv->arenaitem;
691N/A }
691N/A }
691N/A
691N/A return NULL;
691N/A}
691N/A
691N/Aint sp_item_repr_compare_position(SPItem const *first, SPItem const *second)
691N/A{
691N/A return sp_repr_compare_position(first->getRepr(),
691N/A second->getRepr());
691N/A}
691N/A
691N/ASPItem const *sp_item_first_item_child(SPObject const *obj)
691N/A{
691N/A return sp_item_first_item_child( const_cast<SPObject *>(obj) );
691N/A}
691N/A
691N/ASPItem *sp_item_first_item_child(SPObject *obj)
691N/A{
691N/A SPItem *child = 0;
691N/A for ( SPObject *iter = obj->firstChild() ; iter ; iter = iter->next ) {
691N/A if ( SP_IS_ITEM(iter) ) {
691N/A child = SP_ITEM(iter);
691N/A break;
691N/A }
691N/A }
691N/A return child;
691N/A}
691N/A
691N/Avoid SPItem::convert_to_guides() {
691N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
691N/A int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
691N/A
691N/A Geom::OptRect bbox = (prefs_bbox == 0) ? desktopVisualBounds() : desktopGeometricBounds();
691N/A if (!bbox) {
691N/A g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
691N/A return;
691N/A }
691N/A
691N/A std::list<std::pair<Geom::Point, Geom::Point> > pts;
691N/A
691N/A Geom::Point A((*bbox).min());
691N/A Geom::Point C((*bbox).max());
691N/A Geom::Point B(A[Geom::X], C[Geom::Y]);
691N/A Geom::Point D(C[Geom::X], A[Geom::Y]);
691N/A
691N/A pts.push_back(std::make_pair(A, B));
691N/A pts.push_back(std::make_pair(B, C));
691N/A pts.push_back(std::make_pair(C, D));
691N/A pts.push_back(std::make_pair(D, A));
691N/A
691N/A sp_guide_pt_pairs_to_guides(document, pts);
691N/A}
691N/A
691N/A/*
691N/A Local Variables:
691N/A mode:c++
691N/A c-file-style:"stroustrup"
691N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
691N/A indent-tabs-mode:nil
691N/A fill-column:99
691N/A End:
691N/A*/
691N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
691N/A