void spdc_create_single_dot(ToolBase *ec, Geom::Point const &pt, char const *tool, guint event_state) { g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil")); Glib::ustring tool_path = tool; SPDesktop *desktop = ec->desktop; Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc(); Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); repr->setAttribute("sodipodi:type", "arc"); SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr)); Inkscape::GC::release(repr); // apply the tool's current style sp_desktop_apply_style_tool(desktop, repr, tool, false); // find out stroke width (TODO: is there an easier way??) double stroke_width = 3.0; gchar const *style_str = repr->attribute("style"); if (style_str) { SPStyle style(SP_ACTIVE_DOCUMENT); style.mergeString(style_str); stroke_width = style.stroke_width.computed; } // unset stroke and set fill color to former stroke color gchar * str; str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8); repr->setAttribute("style", str); g_free(str); // put the circle where the mouse click occurred and set the diameter to the // current stroke width, multiplied by the amount specified in the preferences Inkscape::Preferences *prefs = Inkscape::Preferences::get(); Geom::Affine const i2d (item->i2dt_affine ()); Geom::Point pp = pt * i2d.inverse(); double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0); if (event_state & GDK_MOD1_MASK) { // TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size // as specified in prefs. Very simple, but it might be sufficient in practice. If not, // we need to devise something more sophisticated. double s = g_random_double_range(-0.5, 0.5); rad *= (1 + s); } if (event_state & GDK_SHIFT_MASK) { // double the point size rad *= 2; } sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]); sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]); sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width); sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width); item->updateRepr(); desktop->getSelection()->set(item); desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot")); DocumentUndo::done(desktop->getDocument(), SP_VERB_NONE, _("Create single dot")); }
void sp_gradient_set_gs2d_matrix(SPGradient *gr, Geom::Affine const &ctm, Geom::Rect const &bbox, Geom::Affine const &gs2d) { gr->gradientTransform = gs2d * ctm.inverse(); if (gr->getUnits() == SP_GRADIENT_UNITS_OBJECTBOUNDINGBOX ) { gr->gradientTransform = ( gr->gradientTransform * Geom::Translate(-bbox.min()) * Geom::Scale(bbox.dimensions()).inverse() ); } gr->gradientTransform_set = TRUE; gr->requestModified(SP_OBJECT_MODIFIED_FLAG); }
/** * Sensing a movement of the original, this function attempts to compensate for it in such a way * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the * clone's transform. */ void SPUse::move_compensate(Geom::Affine const *mp) { // the clone is orphaned; or this is not a real use, but a clone of another use; // we skip it, otherwise duplicate compensation will occur if (this->cloned) { return; } // never compensate uses which are used in flowtext if (this->parent && SP_IS_FLOWREGION(this->parent)) { return; } Inkscape::Preferences *prefs = Inkscape::Preferences::get(); guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL); // user wants no compensation if (mode == SP_CLONE_COMPENSATION_NONE) return; Geom::Affine m(*mp); // this is not a simple move, do not try to compensate if (!(m.isTranslation())) return; // restore item->transform field from the repr, in case it was changed by seltrans this->readAttr ("transform"); Geom::Affine t = this->get_parent_transform(); Geom::Affine clone_move = t.inverse() * m * t; // calculate the compensation matrix and the advertized movement matrix Geom::Affine advertized_move; if (mode == SP_CLONE_COMPENSATION_PARALLEL) { clone_move = clone_move.inverse() * m; advertized_move = m; } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) { clone_move = clone_move.inverse(); advertized_move.setIdentity(); } else { g_assert_not_reached(); } // commit the compensation this->transform *= clone_move; this->doWriteTransform(this->getRepr(), this->transform, &advertized_move); this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); }
Geom::Point KnotHolderEntity::snap_knot_position(Geom::Point const &p, guint state) { if (state & GDK_SHIFT_MASK) { // Don't snap when shift-key is held return p; } Geom::Affine const i2dt (item->i2dt_affine()); Geom::Point s = p * i2dt; SnapManager &m = desktop->namedview->snap_manager; m.setup(desktop, true, item); m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE); m.unSetup(); return s * i2dt.inverse(); }
static void sp_usepath_move_compensate(Geom::Affine const *mp, SPItem *original, SPUsePath *self) { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL); if (mode == SP_CLONE_COMPENSATION_NONE) { return; } SPItem *item = SP_ITEM(self->owner); // TODO kill naughty naughty #if 0 #if 0 Geom::Affine m(*mp); if (!(m.is_translation())) { return; } Geom::Affine const t(item->transform); Geom::Affine clone_move = t.inverse() * m * t; // Calculate the compensation matrix and the advertized movement matrix. Geom::Affine advertized_move; if (mode == SP_CLONE_COMPENSATION_PARALLEL) { //clone_move = clone_move.inverse(); advertized_move.set_identity(); } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) { clone_move = clone_move.inverse() * m; advertized_move = m; } else { g_assert_not_reached(); } // Commit the compensation. item->transform *= clone_move; sp_item_write_transform(item, item->getRepr(), item->transform, &advertized_move); #else (void)mp; (void)original; #endif self->sourceDirty = true; item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); }
Geom::Point KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::SnapConstraint const &constraint, guint state) { if (state & GDK_SHIFT_MASK) { // Don't snap when shift-key is held return p; } Geom::Affine const i2d (item->i2dt_affine()); Geom::Point s = p * i2d; SnapManager &m = desktop->namedview->snap_manager; m.setup(desktop, true, item); // constrainedSnap() will first project the point p onto the constraint line and then try to snap along that line. // This way the constraint is already enforced, no need to worry about that later on Inkscape::Snapper::SnapConstraint transformed_constraint = Inkscape::Snapper::SnapConstraint(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d); m.constrainedSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE, transformed_constraint); m.unSetup(); return s * i2d.inverse(); }
static void sp_offset_move_compensate(Geom::Affine const *mp, SPItem */*original*/, SPOffset *self) { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL); Geom::Affine m(*mp); if (!(m.isTranslation()) || mode == SP_CLONE_COMPENSATION_NONE) { self->sourceDirty=true; self->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); return; } // calculate the compensation matrix and the advertized movement matrix self->readAttr("transform"); Geom::Affine t = self->transform; Geom::Affine offset_move = t.inverse() * m * t; Geom::Affine advertized_move; if (mode == SP_CLONE_COMPENSATION_PARALLEL) { offset_move = offset_move.inverse() * m; advertized_move = m; } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) { offset_move = offset_move.inverse(); advertized_move.setIdentity(); } else { g_assert_not_reached(); } self->sourceDirty=true; // commit the compensation self->transform *= offset_move; self->doWriteTransform(self->getRepr(), self->transform, &advertized_move); self->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); }
/** * Sensing a movement of the original, this function attempts to compensate for it in such a way * that the clone stays unmoved or moves in parallel (depending on user setting) regardless of the * clone's transform. */ void SPUse::move_compensate(Geom::Affine const *mp) { // the clone is orphaned; or this is not a real use, but a clone of another use; // we skip it, otherwise duplicate compensation will occur if (this->cloned) { return; } // never compensate uses which are used in flowtext if (parent && dynamic_cast<SPFlowregion *>(parent)) { return; } Inkscape::Preferences *prefs = Inkscape::Preferences::get(); guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL); // user wants no compensation if (mode == SP_CLONE_COMPENSATION_NONE) return; Geom::Affine m(*mp); Geom::Affine t = this->get_parent_transform(); Geom::Affine clone_move = t.inverse() * m * t; // this is not a simple move, do not try to compensate if (!(m.isTranslation())){ //BUT move clippaths accordingly. //if clone has a clippath, move it accordingly if(clip_ref->getObject()){ SPObject *clip = clip_ref->getObject()->firstChild() ; while(clip){ SPItem *item = (SPItem*) clip; if(item){ item->transform *= m; Geom::Affine identity; item->doWriteTransform(clip->getRepr(),item->transform, &identity); } clip = clip->getNext(); } } if(mask_ref->getObject()){ SPObject *mask = mask_ref->getObject()->firstChild() ; while(mask){ SPItem *item = (SPItem*) mask; if(item){ item->transform *= m; Geom::Affine identity; item->doWriteTransform(mask->getRepr(),item->transform, &identity); } mask = mask->getNext(); } } return; } // restore item->transform field from the repr, in case it was changed by seltrans this->readAttr ("transform"); // calculate the compensation matrix and the advertized movement matrix Geom::Affine advertized_move; if (mode == SP_CLONE_COMPENSATION_PARALLEL) { clone_move = clone_move.inverse() * m; advertized_move = m; } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) { clone_move = clone_move.inverse(); advertized_move.setIdentity(); } else { g_assert_not_reached(); } //if clone has a clippath, move it accordingly if(clip_ref->getObject()){ SPObject *clip = clip_ref->getObject()->firstChild() ; while(clip){ SPItem *item = (SPItem*) clip; if(item){ item->transform *= clone_move.inverse(); Geom::Affine identity; item->doWriteTransform(clip->getRepr(),item->transform, &identity); } clip = clip->getNext(); } } if(mask_ref->getObject()){ SPObject *mask = mask_ref->getObject()->firstChild() ; while(mask){ SPItem *item = (SPItem*) mask; if(item){ item->transform *= clone_move.inverse(); Geom::Affine identity; item->doWriteTransform(mask->getRepr(),item->transform, &identity); } mask = mask->getNext(); } } // commit the compensation this->transform *= clone_move; this->doWriteTransform(this->getRepr(), this->transform, &advertized_move); this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); }
void sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done) { g_return_if_fail (group != NULL); g_return_if_fail (SP_IS_GROUP (group)); SPDocument *doc = group->document; SPRoot *root = doc->getRoot(); SPObject *defs = root->defs; SPItem *gitem = group; Inkscape::XML::Node *grepr = gitem->getRepr(); g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch")); // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse gitem->adjust_paint_recursive (Geom::identity(), Geom::identity(), false); SPItem *pitem = SP_ITEM(gitem->parent); Inkscape::XML::Node *prepr = pitem->getRepr(); if (SP_IS_BOX3D(gitem)) { group = box3d_convert_to_group(SP_BOX3D(gitem)); gitem = group; } sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false); /* Step 1 - generate lists of children objects */ GSList *items = NULL; GSList *objects = NULL; for (SPObject *child = group->firstChild() ; child; child = child->getNext() ) { if (SP_IS_ITEM (child)) { SPItem *citem = SP_ITEM (child); /* Merging of style */ // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do // it here _before_ the new transform is set, so as to use the pre-transform bbox citem->adjust_paint_recursive (Geom::identity(), Geom::identity(), false); sp_style_merge_from_dying_parent(child->style, gitem->style); /* * fixme: We currently make no allowance for the case where child is cloned * and the group has any style settings. * * (This should never occur with documents created solely with the current * version of inkscape without using the XML editor: we usually apply group * style changes to children rather than to the group itself.) * * If the group has no style settings, then * sp_style_merge_from_dying_parent should be a no-op. Otherwise (i.e. if * we change the child's style to compensate for its parent going away) * then those changes will typically be reflected in any clones of child, * whereas we'd prefer for Ungroup not to affect the visual appearance. * * The only way of preserving styling appearance in general is for child to * be put into a new group -- a somewhat surprising response to an Ungroup * command. We could add a new groupmode:transparent that would mostly * hide the existence of such groups from the user (i.e. editing behaves as * if the transparent group's children weren't in a group), though that's * extra complication & maintenance burden and this case is rare. */ child->updateRepr(); Inkscape::XML::Node *nrepr = child->getRepr()->duplicate(prepr->document()); // Merging transform Geom::Affine ctrans; Geom::Affine const g(gitem->transform); if (SP_IS_USE(citem) && sp_use_get_original (SP_USE(citem)) && sp_use_get_original( SP_USE(citem) )->parent == SP_OBJECT(group)) { // make sure a clone's effective transform is the same as was under group ctrans = g.inverse() * citem->transform * g; } else { // We should not apply the group's transformation to both a linked offset AND to its source if (SP_IS_OFFSET(citem)) { // Do we have an offset at hand (whether it's dynamic or linked)? SPItem *source = sp_offset_get_source(SP_OFFSET(citem)); // When dealing with a chain of linked offsets, the transformation of an offset will be // tied to the transformation of the top-most source, not to any of the intermediate // offsets. So let's find the top-most source while (source != NULL && SP_IS_OFFSET(source)) { source = sp_offset_get_source(SP_OFFSET(source)); } if (source != NULL && // If true then we must be dealing with a linked offset ... group->isAncestorOf(source) == false) { // ... of which the source is not in the same group ctrans = citem->transform * g; // then we should apply the transformation of the group to the offset } else { ctrans = citem->transform; } } else { ctrans = citem->transform * g; } } // FIXME: constructing a transform that would fully preserve the appearance of a // textpath if it is ungrouped with its path seems to be impossible in general // case. E.g. if the group was squeezed, to keep the ungrouped textpath squeezed // as well, we'll need to relink it to some "virtual" path which is inversely // stretched relative to the actual path, and then squeeze the textpath back so it // would both fit the actual path _and_ be squeezed as before. It's a bummer. // This is just a way to temporarily remember the transform in repr. When repr is // reattached outside of the group, the transform will be written more properly // (i.e. optimized into the object if the corresponding preference is set) gchar *affinestr=sp_svg_transform_write(ctrans); nrepr->setAttribute("transform", affinestr); g_free(affinestr); items = g_slist_prepend (items, nrepr); } else { Inkscape::XML::Node *nrepr = child->getRepr()->duplicate(prepr->document()); objects = g_slist_prepend (objects, nrepr); } } /* Step 2 - clear group */ // remember the position of the group gint pos = group->getRepr()->position(); // the group is leaving forever, no heir, clones should take note; its children however are going to reemerge group->deleteObject(true, false); /* Step 3 - add nonitems */ if (objects) { Inkscape::XML::Node *last_def = defs->getRepr()->lastChild(); while (objects) { Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data; if (!sp_repr_is_meta_element(repr)) { defs->getRepr()->addChild(repr, last_def); } Inkscape::GC::release(repr); objects = g_slist_remove (objects, objects->data); } } /* Step 4 - add items */ while (items) { Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data; // add item prepr->appendChild(repr); // restore position; since the items list was prepended (i.e. reverse), we now add // all children at the same pos, which inverts the order once again repr->setPosition(pos > 0 ? pos : 0); // fill in the children list if non-null SPItem *item = static_cast<SPItem *>(doc->getObjectByRepr(repr)); item->doWriteTransform(repr, item->transform, NULL, false); Inkscape::GC::release(repr); if (children && SP_IS_ITEM (item)) *children = g_slist_prepend (*children, item); items = g_slist_remove (items, items->data); } if (do_done) { DocumentUndo::done(doc, SP_VERB_NONE, _("Ungroup")); } }