1 /**
2 * \file object-snapper.cpp
3 * \brief Snapping things to objects.
4 *
5 * Authors:
6 * Carl Hetherington <inkscape@carlh.net>
7 * Diederik van Lierop <mail@diedenrezi.nl>
8 *
9 * Copyright (C) 2005 - 2008 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "svg/svg.h"
15 #include "libnr/nr-path.h"
16 #include "libnr/nr-rect-ops.h"
17 #include "libnr/nr-point-fns.h"
18 #include <2geom/path-intersection.h>
19 #include <2geom/point.h>
20 #include <2geom/rect.h>
21 #include "document.h"
22 #include "sp-namedview.h"
23 #include "sp-image.h"
24 #include "sp-item-group.h"
25 #include "sp-item.h"
26 #include "sp-use.h"
27 #include "display/curve.h"
28 #include "desktop.h"
29 #include "inkscape.h"
30 #include "prefs-utils.h"
31 #include "sp-text.h"
32 #include "sp-flowtext.h"
33 #include "text-editing.h"
34 #include "sp-clippath.h"
35 #include "sp-mask.h"
36 #include "helper/geom-curves.h"
38 Inkscape::SnapCandidate::SnapCandidate(SPItem* item, bool clip_or_mask, Geom::Matrix additional_affine)
39 : item(item), clip_or_mask(clip_or_mask), additional_affine(additional_affine)
40 {
41 }
43 Inkscape::SnapCandidate::~SnapCandidate()
44 {
45 }
47 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, Geom::Coord const d)
48 : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true),
49 _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false),
50 _strict_snapping(true), _include_item_center(false)
51 {
52 _candidates = new std::vector<SnapCandidate>;
53 _points_to_snap_to = new std::vector<Geom::Point>;
54 _paths_to_snap_to = new std::vector<Geom::PathVector*>;
55 }
57 Inkscape::ObjectSnapper::~ObjectSnapper()
58 {
59 _candidates->clear();
60 delete _candidates;
62 _points_to_snap_to->clear();
63 delete _points_to_snap_to;
65 _clear_paths();
66 delete _paths_to_snap_to;
67 }
69 /**
70 * Find all items within snapping range.
71 * \param parent Pointer to the document's root, or to a clipped path or mask object
72 * \param it List of items to ignore
73 * \param first_point If true then this point is the first one from a whole bunch of points
74 * \param bbox_to_snap Bounding box hulling the whole bunch of points, all from the same selection and having the same transformation
75 * \param DimensionToSnap Snap in X, Y, or both directions.
76 */
78 void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
79 std::vector<SPItem const *> const *it,
80 bool const &first_point,
81 Geom::Rect const &bbox_to_snap,
82 DimensionToSnap const snap_dim,
83 bool const clip_or_mask,
84 Geom::Matrix const additional_affine) const // transformation of the item being clipped / masked
85 {
86 bool const c1 = (snap_dim == TRANSL_SNAP_XY) && ThisSnapperMightSnap();
87 bool const c2 = (snap_dim != TRANSL_SNAP_XY) && GuidesMightSnap();
89 if (!(c1 || c2)) {
90 return;
91 }
93 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
95 if (first_point) {
96 _candidates->clear();
97 }
99 Geom::Rect bbox_to_snap_incl = bbox_to_snap; // _incl means: will include the snapper tolerance
100 bbox_to_snap_incl.expandBy(getSnapperTolerance()); // see?
102 for (SPObject* o = sp_object_first_child(parent); o != NULL; o = SP_OBJECT_NEXT(o)) {
103 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(desktop->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
104 // Don't snap to locked items, and
105 // don't snap to hidden objects, unless they're a clipped path or a mask
106 /* See if this item is on the ignore list */
107 std::vector<SPItem const *>::const_iterator i;
108 if (it != NULL) {
109 i = it->begin();
110 while (i != it->end() && *i != o) {
111 i++;
112 }
113 }
115 if (it == NULL || i == it->end()) {
116 SPItem *item = SP_ITEM(o);
117 Geom::Matrix transform = Geom::identity();
118 if (item) {
119 SPObject *obj = NULL;
120 if (clip_or_mask) { // If the current item is a clipping path or a mask
121 // then store the transformation of the clipped path or mask itself
122 // but also take into account the additional affine of the object
123 // being clipped / masked
124 transform = to_2geom(item->transform) * additional_affine;
125 } else { // cannot clip or mask more than once
126 // The current item is not a clipping path or a mask, but might
127 // still be the subject of clipping or masking itself ; if so, then
128 // we should also consider that path or mask for snapping to
129 obj = SP_OBJECT(item->clip_ref->getObject());
130 if (obj) {
131 _findCandidates(obj, it, false, bbox_to_snap, snap_dim, true, item->transform);
132 }
133 obj = SP_OBJECT(item->mask_ref->getObject());
134 if (obj) {
135 _findCandidates(obj, it, false, bbox_to_snap, snap_dim, true, item->transform);
136 }
137 }
138 }
140 if (SP_IS_GROUP(o)) {
141 _findCandidates(o, it, false, bbox_to_snap, snap_dim, false, Geom::identity());
142 } else {
143 boost::optional<NR::Rect> bbox_of_item = NR::Rect();
144 if (clip_or_mask) {
145 // Oh oh, this will get ugly. We cannot use sp_item_i2d_affine directly because we need to
146 // insert an additional transformation in document coordinates (code copied from sp_item_i2d_affine)
147 sp_item_invoke_bbox(item,
148 &bbox_of_item,
149 from_2geom(to_2geom(sp_item_i2doc_affine(item)) * matrix_to_desktop(additional_affine, item)),
150 true);
151 } else {
152 sp_item_invoke_bbox(item, &bbox_of_item, sp_item_i2d_affine(item), true);
153 }
154 if (bbox_of_item) {
155 // See if the item is within range
156 if (bbox_to_snap_incl.intersects(to_2geom(*bbox_of_item))) {
157 // This item is within snapping range, so record it as a candidate
158 _candidates->push_back(SnapCandidate(item, clip_or_mask, additional_affine));
159 }
160 }
161 }
162 }
163 }
164 }
165 }
168 void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &t,
169 bool const &first_point) const
170 {
171 // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
172 // e.g. when translating an item using the selector tool, then we will only do this for the
173 // first point and store the collection for later use. This significantly improves the performance
174 if (first_point) {
175 _points_to_snap_to->clear();
177 // Determine the type of bounding box we should snap to
178 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
180 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
181 bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
182 bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
184 // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
185 g_assert(!(p_is_a_node && p_is_a_bbox || p_is_a_bbox && p_is_a_guide || p_is_a_node && p_is_a_guide));
187 if (_snap_to_bboxnode) {
188 int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
189 bbox_type = (prefs_bbox == 0)?
190 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
191 }
193 // Consider the page border for snapping
194 if (_snap_to_page_border) {
195 _getBorderNodes(_points_to_snap_to);
196 }
198 for (std::vector<SnapCandidate>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
199 //Geom::Matrix i2doc(Geom::identity());
200 SPItem *root_item = (*i).item;
201 if (SP_IS_USE((*i).item)) {
202 root_item = sp_use_root(SP_USE((*i).item));
203 }
204 g_return_if_fail(root_item);
206 //Collect all nodes so we can snap to them
207 if (_snap_to_itemnode) {
208 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
209 std::vector<NR::Point> dummy_vctr;
210 sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(dummy_vctr));
211 to_2geom(dummy_vctr, *_points_to_snap_to);
212 }
213 }
215 //Collect the bounding box's corners so we can snap to them
216 if (_snap_to_bboxnode) {
217 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
218 // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
219 // of the item AND the bbox of the clipping path at the same time
220 if (!(*i).clip_or_mask) {
221 boost::optional<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
222 if (b) {
223 for ( unsigned k = 0 ; k < 4 ; k++ ) {
224 _points_to_snap_to->push_back(to_2geom(b->corner(k)));
225 }
226 }
227 }
228 }
229 }
230 }
231 }
232 }
234 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
235 Inkscape::Snapper::PointType const &t,
236 Geom::Point const &p,
237 bool const &first_point,
238 std::vector<Geom::Point> *unselected_nodes) const
239 {
240 // Iterate through all nodes, find out which one is the closest to p, and snap to it!
242 _collectNodes(t, first_point);
244 if (unselected_nodes != NULL) {
245 _points_to_snap_to->insert(_points_to_snap_to->end(), unselected_nodes->begin(), unselected_nodes->end());
246 }
248 SnappedPoint s;
249 bool success = false;
251 for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
252 Geom::Coord dist = Geom::L2(*k - p);
253 if (dist < getSnapperTolerance() && dist < s.getDistance()) {
254 s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
255 success = true;
256 }
257 }
259 if (success) {
260 sc.points.push_back(s);
261 }
262 }
264 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
265 Inkscape::Snapper::PointType const &t,
266 Geom::Point const &p,
267 Geom::Point const &guide_normal) const
268 {
269 // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
270 _collectNodes(t, true);
272 SnappedPoint s;
273 bool success = false;
275 Geom::Coord tol = getSnapperTolerance();
277 for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
278 // Project each node (*k) on the guide line (running through point p)
279 Geom::Point p_proj = project_on_linesegment(*k, p, p + Geom::rot90(guide_normal));
280 Geom::Coord dist = Geom::L2(*k - p_proj); // distance from node to the guide
281 Geom::Coord dist2 = Geom::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
282 if ((dist < tol && dist2 < tol || getSnapperAlwaysSnap()) && dist < s.getDistance()) {
283 s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap());
284 success = true;
285 }
286 }
288 if (success) {
289 sc.points.push_back(s);
290 }
291 }
294 /**
295 * Returns index of first NR_END bpath in array.
296 */
298 /* Obsolete
299 static unsigned sp_bpath_length(NArtBpath const bpath[])
300 {
301 g_return_val_if_fail(bpath != NULL, FALSE);
302 unsigned ret = 0;
303 while ( bpath[ret].code != NR_END ) {
304 ++ret;
305 }
306 ++ret;
307 return ret;
308 }*/
310 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
311 bool const &first_point) const
312 {
313 // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
314 // e.g. when translating an item using the selector tool, then we will only do this for the
315 // first point and store the collection for later use. This significantly improves the performance
316 if (first_point) {
317 _clear_paths();
319 // Determine the type of bounding box we should snap to
320 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
322 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
324 if (_snap_to_bboxpath) {
325 int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
326 bbox_type = (prefs_bbox ==0)?
327 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
328 }
330 // Consider the page border for snapping
331 if (_snap_to_page_border) {
332 Geom::PathVector *border_path = _getBorderPathv();
333 if (border_path != NULL) {
334 _paths_to_snap_to->push_back(border_path);
335 }
336 }
338 for (std::vector<SnapCandidate>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
340 /* Transform the requested snap point to this item's coordinates */
341 Geom::Matrix i2doc(Geom::identity());
342 SPItem *root_item = NULL;
343 /* We might have a clone at hand, so make sure we get the root item */
344 if (SP_IS_USE((*i).item)) {
345 i2doc = sp_use_get_root_transform(SP_USE((*i).item));
346 root_item = sp_use_root(SP_USE((*i).item));
347 g_return_if_fail(root_item);
348 } else {
349 i2doc = sp_item_i2doc_affine((*i).item);
350 root_item = (*i).item;
351 }
353 //Build a list of all paths considered for snapping to
355 //Add the item's path to snap to
356 if (_snap_to_itempath) {
357 if (!(_strict_snapping && !p_is_a_node)) {
358 // Snapping to the path of characters is very cool, but for a large
359 // chunk of text this will take ages! So limit snapping to text paths
360 // containing max. 240 characters. Snapping the bbox will not be affected
361 bool very_lenghty_prose = false;
362 if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
363 very_lenghty_prose = sp_text_get_length(SP_TEXT(root_item)) > 240;
364 }
365 // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
366 // which corresponds to a lag of 500 msec. This is for snapping a rect
367 // to a single line of text.
369 // Snapping for example to a traced bitmap is also very stressing for
370 // the CPU, so we'll only snap to paths having no more than 500 nodes
371 // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
372 bool very_complex_path = false;
373 if (SP_IS_PATH(root_item)) {
374 very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
375 }
377 if (!very_lenghty_prose && !very_complex_path) {
378 SPCurve *curve = curve_for_item(root_item);
379 if (curve) {
380 // We will get our own copy of the path, which must be freed at some point
381 Geom::PathVector *borderpathv = pathvector_for_curve(root_item, curve, true, true, Geom::identity(), (*i).additional_affine);
382 _paths_to_snap_to->push_back(borderpathv); // Perhaps for speed, get a reference to the Geom::pathvector, and store the transformation besides it.
383 curve->unref();
384 }
385 }
386 }
387 }
389 //Add the item's bounding box to snap to
390 if (_snap_to_bboxpath) {
391 if (!(_strict_snapping && p_is_a_node)) {
392 // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
393 // of the item AND the bbox of the clipping path at the same time
394 if (!(*i).clip_or_mask) {
395 NRRect rect;
396 sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
397 Geom::Rect const rect2 = to_2geom(*rect.upgrade());
398 Geom::PathVector *path = _getPathvFromRect(rect2);
399 _paths_to_snap_to->push_back(path);
400 }
401 }
402 }
403 }
404 }
405 }
407 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
408 Inkscape::Snapper::PointType const &t,
409 Geom::Point const &p,
410 bool const &first_point,
411 std::vector<Geom::Point> *unselected_nodes,
412 SPPath const *selected_path) const
413 {
414 _collectPaths(t, first_point);
415 // Now we can finally do the real snapping, using the paths collected above
417 /* FIXME: this seems like a hack. Perhaps Snappers should be
418 ** in SPDesktop rather than SPNamedView?
419 */
420 // TODO Diederik: shouldn't we just make all snapping code use document
421 // coordinates instead? Then we won't need a pointer to the desktop any longer
422 // At least we should define a clear boundary between those different coordinates,
423 // now this is not well defined
425 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
426 Geom::Point const p_doc = desktop->dt2doc(p);
428 bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
430 if (first_point) {
431 /* While editing a path in the node tool, findCandidates must ignore that path because
432 * of the node snapping requirements (i.e. only unselected nodes must be snapable).
433 * This path must not be ignored however when snapping to the paths, so we add it here
434 * manually when applicable.
435 *
436 * Note that this path must be the last in line!
437 * */
438 if (node_tool_active) {
439 SPCurve *curve = curve_for_item(SP_ITEM(selected_path));
440 if (curve) {
441 Geom::PathVector *pathv = pathvector_for_curve(SP_ITEM(selected_path), curve, true, true, Geom::identity(), Geom::identity()); // We will get our own copy of the path, which must be freed at some point
442 _paths_to_snap_to->push_back(pathv);
443 curve->unref();
444 }
445 }
446 }
448 for (std::vector<Geom::PathVector*>::const_iterator it_p = _paths_to_snap_to->begin(); it_p != _paths_to_snap_to->end(); it_p++) {
449 bool const being_edited = (node_tool_active && (*it_p) == _paths_to_snap_to->back());
450 //if true then this pathvector it_pv is currently being edited in the node tool
452 // char * svgd = sp_svg_write_path(**it_p);
453 // std::cout << "Dumping the pathvector: " << svgd << std::endl;
455 for(Geom::PathVector::iterator it_pv = (*it_p)->begin(); it_pv != (*it_p)->end(); ++it_pv) {
456 std::vector<double> anp;
458 // Find a nearest point for each curve within this path
459 // (path->allNearestPoints() will not do this for us! It was originally
460 // intended to find for example multiple equidistant solutions)
461 unsigned int num_curves = (*it_pv).size();
462 if ( (*it_pv).closed() ) ++num_curves;
463 for (double t = 0; (t+1) <= double(num_curves); t++) {
464 // Find a nearest point with time value in the range [t, t+1]
465 anp.push_back((*it_pv).nearestPoint(p_doc, t, t+1));
466 }
468 for (std::vector<double>::const_iterator np = anp.begin(); np != anp.end(); np++) {
469 bool c1 = true;
470 bool c2 = true;
471 Geom::Point start_pt = desktop->doc2dt((*it_pv).pointAt(floor(*np)));
472 Geom::Point end_pt = desktop->doc2dt((*it_pv).pointAt(ceil(*np)));
474 if (being_edited) {
475 /* If the path is being edited, then we should only snap though to stationary pieces of the path
476 * and not to the pieces that are being dragged around. This way we avoid
477 * self-snapping. For this we check whether the nodes at both ends of the current
478 * piece are unselected; if they are then this piece must be stationary
479 */
480 g_assert(unselected_nodes != NULL);
481 c1 = isUnselectedNode(start_pt, unselected_nodes);
482 c2 = isUnselectedNode(end_pt, unselected_nodes);
483 }
485 Geom::Point const sp_doc = (*it_pv).pointAt(*np);
486 Geom::Point const sp_dt = desktop->doc2dt(sp_doc);
488 if (!being_edited || (c1 && c2)) {
489 Geom::Coord const dist = Geom::distance(sp_doc, p_doc);
490 if (dist < getSnapperTolerance()) {
491 double t = MIN(*np, (*it_pv).size()); // make sure that t is within bounds;
492 Geom::Curve const *curve = &((*it_pv).at_index(int(t)));
493 sc.curves.push_back(Inkscape::SnappedCurve(from_2geom(sp_dt), dist, getSnapperTolerance(), getSnapperAlwaysSnap(), curve));
494 }
495 }
496 }
497 } // End of: for (Geom::PathVector::iterator ....)
498 }
499 }
501 /* Returns true if point is coincident with one of the unselected nodes */
502 bool Inkscape::ObjectSnapper::isUnselectedNode(Geom::Point const &point, std::vector<Geom::Point> const *unselected_nodes) const
503 {
504 if (unselected_nodes == NULL) {
505 return false;
506 }
508 if (unselected_nodes->size() == 0) {
509 return false;
510 }
512 for (std::vector<Geom::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
513 if (Geom::L2(point - *i) < 1e-4) {
514 return true;
515 }
516 }
518 return false;
519 }
521 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
522 Inkscape::Snapper::PointType const &t,
523 Geom::Point const &p,
524 bool const &first_point,
525 ConstraintLine const &c) const
526 {
528 _collectPaths(t, first_point);
530 // Now we can finally do the real snapping, using the paths collected above
532 /* FIXME: this seems like a hack. Perhaps Snappers should be
533 ** in SPDesktop rather than SPNamedView?
534 */
535 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
536 Geom::Point const p_doc = desktop->dt2doc(p);
538 Geom::Point direction_vector = c.getDirection();
539 if (!is_zero(direction_vector)) {
540 direction_vector = Geom::unit_vector(direction_vector);
541 }
543 Geom::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
544 Geom::Point const p2_on_cl = p1_on_cl + direction_vector;
546 // The intersection point of the constraint line with any path,
547 // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
548 // The distance between those points is twice the snapping tolerance
549 Geom::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
550 Geom::Point const p_min_on_cl = desktop->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);
551 Geom::Point const p_max_on_cl = desktop->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
553 Geom::Path cl;
554 std::vector<Geom::Path> clv;
555 cl.start(p_min_on_cl);
556 cl.appendNew<Geom::LineSegment>(p_max_on_cl);
557 clv.push_back(cl);
559 for (std::vector<Geom::PathVector*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
560 if (*k) {
561 Geom::CrossingSet cs = Geom::crossings(clv, *(*k));
562 if (cs.size() > 0) {
563 // We need only the first element of cs, because cl is only a single straight linesegment
564 // This first element contains a vector filled with crossings of cl with *k
565 for (std::vector<Geom::Crossing>::const_iterator m = cs[0].begin(); m != cs[0].end(); m++) {
566 if ((*m).ta >= 0 && (*m).ta <= 1 ) {
567 // Reconstruct the point of intersection
568 Geom::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl);
569 // When it's within snapping range, then return it
570 // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1)
571 Geom::Coord dist = Geom::L2(desktop->dt2doc(p_proj_on_cl) - p_inters);
572 SnappedPoint s(desktop->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
573 sc.points.push_back(s);
574 }
575 }
576 }
577 }
578 }
579 }
582 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
583 Inkscape::Snapper::PointType const &t,
584 Geom::Point const &p,
585 bool const &first_point,
586 boost::optional<Geom::Rect> const &bbox_to_snap,
587 std::vector<SPItem const *> const *it,
588 std::vector<Geom::Point> *unselected_nodes) const
589 {
590 if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
591 return;
592 }
594 /* Get a list of all the SPItems that we will try to snap to */
595 if (first_point) {
596 Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
597 _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
598 }
600 if (_snap_to_itemnode || _snap_to_bboxnode || _snap_to_page_border) {
601 _snapNodes(sc, t, p, first_point, unselected_nodes);
602 }
604 if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
605 unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
606 if (n > 0) {
607 /* While editing a path in the node tool, findCandidates must ignore that path because
608 * of the node snapping requirements (i.e. only unselected nodes must be snapable).
609 * That path must not be ignored however when snapping to the paths, so we add it here
610 * manually when applicable
611 */
612 SPPath *path = NULL;
613 if (it != NULL) {
614 g_assert(SP_IS_PATH(*it->begin()));
615 g_assert(it->size() == 1);
616 path = SP_PATH(*it->begin());
617 }
618 _snapPaths(sc, t, p, first_point, unselected_nodes, path);
619 } else {
620 _snapPaths(sc, t, p, first_point, NULL, NULL);
621 }
622 }
623 }
625 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
626 Inkscape::Snapper::PointType const &t,
627 Geom::Point const &p,
628 bool const &first_point,
629 boost::optional<Geom::Rect> const &bbox_to_snap,
630 ConstraintLine const &c,
631 std::vector<SPItem const *> const *it) const
632 {
633 if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
634 return;
635 }
637 /* Get a list of all the SPItems that we will try to snap to */
638 if (first_point) {
639 Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
640 _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
641 }
643 // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
644 // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
645 // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
647 // When snapping to objects, we either snap to their nodes or their paths. It is however very
648 // unlikely that any node will be exactly at the constrained line, so for a constrained snap
649 // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
650 // so we will more or less snap to them anyhow.
652 if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
653 _snapPathsConstrained(sc, t, p, first_point, c);
654 }
655 }
658 // This method is used to snap a guide to nodes, while dragging the guide around
659 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
660 Geom::Point const &p,
661 Geom::Point const &guide_normal) const
662 {
663 if ( NULL == _named_view ) {
664 return;
665 }
667 /* Get a list of all the SPItems that we will try to snap to */
668 std::vector<SPItem*> cand;
669 std::vector<SPItem const *> const it; //just an empty list
671 DimensionToSnap snap_dim;
672 if (guide_normal == to_2geom(component_vectors[Geom::Y])) {
673 snap_dim = GUIDE_TRANSL_SNAP_Y;
674 } else if (guide_normal == to_2geom(component_vectors[Geom::X])) {
675 snap_dim = GUIDE_TRANSL_SNAP_X;
676 } else {
677 snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
678 }
680 // We don't support ANGLED_GUIDE_ROT_SNAP yet.
682 // It would be cool to allow the user to rotate a guide by dragging it, instead of
683 // only translating it. (For example when CTRL is pressed). We will need an UI part
684 // for that first; and some important usability choices need to be made:
685 // E.g. which point should be used for pivoting? A previously snapped point,
686 // or a transformation center (which can be moved after clicking for the
687 // second time on an object; but should this point then be constrained to the
688 // line, or can it be located anywhere?)
690 _findCandidates(sp_document_root(_named_view->document), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
691 _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
692 // _snapRotatingGuideToNodes has not been implemented yet.
693 }
695 /**
696 * \return true if this Snapper will snap at least one kind of point.
697 */
698 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
699 {
700 bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
701 return (_snap_enabled && _snap_from != 0 && snap_to_something);
702 }
704 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
705 {
706 bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
707 return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
708 }
710 void Inkscape::ObjectSnapper::_clear_paths() const
711 {
712 for (std::vector<Geom::PathVector*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
713 g_free(*k);
714 }
715 _paths_to_snap_to->clear();
716 }
718 Geom::PathVector* Inkscape::ObjectSnapper::_getBorderPathv() const
719 {
720 Geom::Rect const border_rect = Geom::Rect(Geom::Point(0,0), Geom::Point(sp_document_width(_named_view->document),sp_document_height(_named_view->document)));
721 return _getPathvFromRect(border_rect);
722 }
724 Geom::PathVector* Inkscape::ObjectSnapper::_getPathvFromRect(Geom::Rect const rect) const
725 {
726 SPCurve const *border_curve = SPCurve::new_from_rect(rect);
727 if (border_curve) {
728 Geom::PathVector *dummy = new Geom::PathVector(border_curve->get_pathvector());
729 return dummy;
730 } else {
731 return NULL;
732 }
733 }
735 void Inkscape::ObjectSnapper::_getBorderNodes(std::vector<Geom::Point> *points) const
736 {
737 Geom::Coord w = sp_document_width(_named_view->document);
738 Geom::Coord h = sp_document_height(_named_view->document);
739 points->push_back(Geom::Point(0,0));
740 points->push_back(Geom::Point(0,h));
741 points->push_back(Geom::Point(w,h));
742 points->push_back(Geom::Point(w,0));
743 }
745 /*
746 Local Variables:
747 mode:c++
748 c-file-style:"stroustrup"
749 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
750 indent-tabs-mode:nil
751 fill-column:99
752 End:
753 */
754 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :