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-rect-ops.h"
16 #include "libnr/nr-point-fns.h"
17 #include <2geom/path-intersection.h>
18 #include <2geom/point.h>
19 #include <2geom/rect.h>
20 #include "document.h"
21 #include "sp-namedview.h"
22 #include "sp-image.h"
23 #include "sp-item-group.h"
24 #include "sp-item.h"
25 #include "sp-use.h"
26 #include "display/curve.h"
27 #include "inkscape.h"
28 #include "preferences.h"
29 #include "sp-text.h"
30 #include "sp-flowtext.h"
31 #include "text-editing.h"
32 #include "sp-clippath.h"
33 #include "sp-mask.h"
34 #include "helper/geom-curves.h"
35 #include "desktop.h"
37 Inkscape::SnapCandidate::SnapCandidate(SPItem* item, bool clip_or_mask, Geom::Matrix additional_affine)
38 : item(item), clip_or_mask(clip_or_mask), additional_affine(additional_affine)
39 {
40 }
42 Inkscape::SnapCandidate::~SnapCandidate()
43 {
44 }
46 Inkscape::ObjectSnapper::ObjectSnapper(SnapManager const *sm, Geom::Coord const d)
47 : Snapper(sm, d), _snap_to_itemnode(true), _snap_to_itempath(true),
48 _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false),
49 _strict_snapping(true)
50 {
51 _candidates = new std::vector<SnapCandidate>;
52 _points_to_snap_to = new std::vector<Geom::Point>;
53 _paths_to_snap_to = new std::vector<Geom::PathVector*>;
54 }
56 Inkscape::ObjectSnapper::~ObjectSnapper()
57 {
58 _candidates->clear();
59 delete _candidates;
61 _points_to_snap_to->clear();
62 delete _points_to_snap_to;
64 _clear_paths();
65 delete _paths_to_snap_to;
66 }
68 /**
69 * Find all items within snapping range.
70 * \param parent Pointer to the document's root, or to a clipped path or mask object
71 * \param it List of items to ignore
72 * \param first_point If true then this point is the first one from a whole bunch of points
73 * \param bbox_to_snap Bounding box hulling the whole bunch of points, all from the same selection and having the same transformation
74 * \param DimensionToSnap Snap in X, Y, or both directions.
75 */
77 void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
78 std::vector<SPItem const *> const *it,
79 bool const &first_point,
80 Geom::Rect const &bbox_to_snap,
81 DimensionToSnap const snap_dim,
82 bool const clip_or_mask,
83 Geom::Matrix const additional_affine) const // transformation of the item being clipped / masked
84 {
85 bool const c1 = (snap_dim == TRANSL_SNAP_XY) && ThisSnapperMightSnap();
86 bool const c2 = (snap_dim != TRANSL_SNAP_XY) && GuidesMightSnap();
88 if (!(c1 || c2)) {
89 return;
90 }
92 if (first_point) {
93 _candidates->clear();
94 }
96 Geom::Rect bbox_to_snap_incl = bbox_to_snap; // _incl means: will include the snapper tolerance
97 bbox_to_snap_incl.expandBy(getSnapperTolerance()); // see?
99 for (SPObject* o = sp_object_first_child(parent); o != NULL; o = SP_OBJECT_NEXT(o)) {
100 g_assert(_snapmanager->getDesktop() != NULL);
101 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(_snapmanager->getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
102 // Don't snap to locked items, and
103 // don't snap to hidden objects, unless they're a clipped path or a mask
104 /* See if this item is on the ignore list */
105 std::vector<SPItem const *>::const_iterator i;
106 if (it != NULL) {
107 i = it->begin();
108 while (i != it->end() && *i != o) {
109 i++;
110 }
111 }
113 if (it == NULL || i == it->end()) {
114 SPItem *item = SP_ITEM(o);
115 Geom::Matrix transform = Geom::identity();
116 if (item) {
117 SPObject *obj = NULL;
118 if (clip_or_mask) { // If the current item is a clipping path or a mask
119 // then store the transformation of the clipped path or mask itself
120 // but also take into account the additional affine of the object
121 // being clipped / masked
122 transform = to_2geom(item->transform) * additional_affine;
123 } else { // cannot clip or mask more than once
124 // The current item is not a clipping path or a mask, but might
125 // still be the subject of clipping or masking itself ; if so, then
126 // we should also consider that path or mask for snapping to
127 obj = SP_OBJECT(item->clip_ref->getObject());
128 if (obj) {
129 _findCandidates(obj, it, false, bbox_to_snap, snap_dim, true, item->transform);
130 }
131 obj = SP_OBJECT(item->mask_ref->getObject());
132 if (obj) {
133 _findCandidates(obj, it, false, bbox_to_snap, snap_dim, true, item->transform);
134 }
135 }
136 }
138 if (SP_IS_GROUP(o)) {
139 _findCandidates(o, it, false, bbox_to_snap, snap_dim, false, Geom::identity());
140 } else {
141 Geom::OptRect bbox_of_item = Geom::Rect();
142 if (clip_or_mask) {
143 // Oh oh, this will get ugly. We cannot use sp_item_i2d_affine directly because we need to
144 // insert an additional transformation in document coordinates (code copied from sp_item_i2d_affine)
145 sp_item_invoke_bbox(item,
146 bbox_of_item,
147 sp_item_i2doc_affine(item) * matrix_to_desktop(additional_affine, item),
148 true);
149 } else {
150 sp_item_invoke_bbox(item, bbox_of_item, sp_item_i2d_affine(item), true);
151 }
152 if (bbox_of_item) {
153 // See if the item is within range
154 if (bbox_to_snap_incl.intersects(*bbox_of_item)) {
155 // This item is within snapping range, so record it as a candidate
156 _candidates->push_back(SnapCandidate(item, clip_or_mask, additional_affine));
157 }
158 }
159 }
160 }
161 }
162 }
163 }
166 void Inkscape::ObjectSnapper::_collectNodes(Inkscape::SnapPreferences::PointType const &t,
167 bool const &first_point) const
168 {
169 // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
170 // e.g. when translating an item using the selector tool, then we will only do this for the
171 // first point and store the collection for later use. This significantly improves the performance
172 if (first_point) {
173 _points_to_snap_to->clear();
175 // Determine the type of bounding box we should snap to
176 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
178 bool p_is_a_node = t & Inkscape::SnapPreferences::SNAPPOINT_NODE;
179 bool p_is_a_bbox = t & Inkscape::SnapPreferences::SNAPPOINT_BBOX;
180 bool p_is_a_guide = t & Inkscape::SnapPreferences::SNAPPOINT_GUIDE;
182 // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
183 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)));
185 if (_snap_to_bboxnode) {
186 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
187 bool prefs_bbox = prefs->getBool("/tools/bounding_box");
188 bbox_type = !prefs_bbox ?
189 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
190 }
192 // Consider the page border for snapping
193 if (_snap_to_page_border) {
194 _getBorderNodes(_points_to_snap_to);
195 }
197 for (std::vector<SnapCandidate>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
198 //Geom::Matrix i2doc(Geom::identity());
199 SPItem *root_item = (*i).item;
200 if (SP_IS_USE((*i).item)) {
201 root_item = sp_use_root(SP_USE((*i).item));
202 }
203 g_return_if_fail(root_item);
205 //Collect all nodes so we can snap to them
206 if (_snap_to_itemnode) {
207 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
208 sp_item_snappoints(root_item, SnapPointsIter(*_points_to_snap_to), &_snapmanager->snapprefs);
209 }
210 }
212 //Collect the bounding box's corners so we can snap to them
213 if (_snap_to_bboxnode) {
214 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
215 // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
216 // of the item AND the bbox of the clipping path at the same time
217 if (!(*i).clip_or_mask) {
218 Geom::OptRect b = sp_item_bbox_desktop(root_item, bbox_type);
219 if (b) {
220 for ( unsigned k = 0 ; k < 4 ; k++ ) {
221 _points_to_snap_to->push_back(b->corner(k));
222 }
223 }
224 }
225 }
226 }
227 }
228 }
229 }
231 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
232 Inkscape::SnapPreferences::PointType const &t,
233 Geom::Point const &p,
234 bool const &first_point,
235 std::vector<Geom::Point> *unselected_nodes) const
236 {
237 // Iterate through all nodes, find out which one is the closest to p, and snap to it!
239 _collectNodes(t, first_point);
241 if (unselected_nodes != NULL) {
242 _points_to_snap_to->insert(_points_to_snap_to->end(), unselected_nodes->begin(), unselected_nodes->end());
243 }
245 SnappedPoint s;
246 bool success = false;
248 for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
249 Geom::Coord dist = Geom::L2(*k - p);
250 if (dist < getSnapperTolerance() && dist < s.getSnapDistance()) {
251 s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true);
252 success = true;
253 }
254 }
256 if (success) {
257 sc.points.push_back(s);
258 }
259 }
261 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
262 Inkscape::SnapPreferences::PointType const &t,
263 Geom::Point const &p,
264 Geom::Point const &guide_normal) const
265 {
266 // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
267 _collectNodes(t, true);
269 SnappedPoint s;
270 bool success = false;
272 Geom::Coord tol = getSnapperTolerance();
274 for (std::vector<Geom::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
275 // Project each node (*k) on the guide line (running through point p)
276 Geom::Point p_proj = project_on_linesegment(*k, p, p + Geom::rot90(guide_normal));
277 Geom::Coord dist = Geom::L2(*k - p_proj); // distance from node to the guide
278 Geom::Coord dist2 = Geom::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
279 if ((dist < tol && dist2 < tol) || (getSnapperAlwaysSnap() && dist < s.getSnapDistance())) {
280 s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap(), true);
281 success = true;
282 }
283 }
285 if (success) {
286 sc.points.push_back(s);
287 }
288 }
291 /**
292 * Returns index of first NR_END bpath in array.
293 */
295 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::SnapPreferences::PointType const &t,
296 bool const &first_point) const
297 {
298 // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
299 // e.g. when translating an item using the selector tool, then we will only do this for the
300 // first point and store the collection for later use. This significantly improves the performance
301 if (first_point) {
302 _clear_paths();
304 // Determine the type of bounding box we should snap to
305 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
307 bool p_is_a_node = t & Inkscape::SnapPreferences::SNAPPOINT_NODE;
309 if (_snap_to_bboxpath) {
310 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
311 int prefs_bbox = prefs->getBool("/tools/bounding_box", 0);
312 bbox_type = !prefs_bbox ?
313 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
314 }
316 // Consider the page border for snapping
317 if (_snap_to_page_border) {
318 Geom::PathVector *border_path = _getBorderPathv();
319 if (border_path != NULL) {
320 _paths_to_snap_to->push_back(border_path);
321 }
322 }
324 for (std::vector<SnapCandidate>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
326 /* Transform the requested snap point to this item's coordinates */
327 Geom::Matrix i2doc(Geom::identity());
328 SPItem *root_item = NULL;
329 /* We might have a clone at hand, so make sure we get the root item */
330 if (SP_IS_USE((*i).item)) {
331 i2doc = sp_use_get_root_transform(SP_USE((*i).item));
332 root_item = sp_use_root(SP_USE((*i).item));
333 g_return_if_fail(root_item);
334 } else {
335 i2doc = sp_item_i2doc_affine((*i).item);
336 root_item = (*i).item;
337 }
339 //Build a list of all paths considered for snapping to
341 //Add the item's path to snap to
342 if (_snap_to_itempath) {
343 if (!(_strict_snapping && !p_is_a_node)) {
344 // Snapping to the path of characters is very cool, but for a large
345 // chunk of text this will take ages! So limit snapping to text paths
346 // containing max. 240 characters. Snapping the bbox will not be affected
347 bool very_lenghty_prose = false;
348 if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
349 very_lenghty_prose = sp_text_get_length(SP_TEXT(root_item)) > 240;
350 }
351 // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
352 // which corresponds to a lag of 500 msec. This is for snapping a rect
353 // to a single line of text.
355 // Snapping for example to a traced bitmap is also very stressing for
356 // the CPU, so we'll only snap to paths having no more than 500 nodes
357 // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
358 bool very_complex_path = false;
359 if (SP_IS_PATH(root_item)) {
360 very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
361 }
363 if (!very_lenghty_prose && !very_complex_path) {
364 SPCurve *curve = curve_for_item(root_item);
365 if (curve) {
366 // We will get our own copy of the path, which must be freed at some point
367 Geom::PathVector *borderpathv = pathvector_for_curve(root_item, curve, true, true, Geom::identity(), (*i).additional_affine);
368 _paths_to_snap_to->push_back(borderpathv); // Perhaps for speed, get a reference to the Geom::pathvector, and store the transformation besides it.
369 curve->unref();
370 }
371 }
372 }
373 }
375 //Add the item's bounding box to snap to
376 if (_snap_to_bboxpath) {
377 if (!(_strict_snapping && p_is_a_node)) {
378 // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
379 // of the item AND the bbox of the clipping path at the same time
380 if (!(*i).clip_or_mask) {
381 Geom::OptRect rect;
382 sp_item_invoke_bbox(root_item, rect, i2doc, TRUE, bbox_type);
383 if (rect) {
384 Geom::PathVector *path = _getPathvFromRect(*rect);
385 _paths_to_snap_to->push_back(path);
386 }
387 }
388 }
389 }
390 }
391 }
392 }
394 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
395 Inkscape::SnapPreferences::PointType const &t,
396 Geom::Point const &p,
397 bool const &first_point,
398 std::vector<Geom::Point> *unselected_nodes,
399 SPPath const *selected_path) const
400 {
401 _collectPaths(t, first_point);
402 // Now we can finally do the real snapping, using the paths collected above
404 g_assert(_snapmanager->getDesktop() != NULL);
405 Geom::Point const p_doc = _snapmanager->getDesktop()->dt2doc(p);
407 bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
409 if (first_point) {
410 /* findCandidates() is used for snapping to both paths and nodes. It ignores the path that is
411 * currently being edited, because that path requires special care: when snapping to nodes
412 * only the unselected nodes of that path should be considered, and these will be passed on separately.
413 * This path must not be ignored however when snapping to the paths, so we add it here
414 * manually when applicable.
415 *
416 * Note that this path must be the last in line!
417 * */
418 if (node_tool_active) {
419 SPCurve *curve = curve_for_item(SP_ITEM(selected_path));
420 if (curve) {
421 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
422 _paths_to_snap_to->push_back(pathv);
423 curve->unref();
424 }
425 }
426 }
428 for (std::vector<Geom::PathVector*>::const_iterator it_p = _paths_to_snap_to->begin(); it_p != _paths_to_snap_to->end(); it_p++) {
429 bool const being_edited = (node_tool_active && (*it_p) == _paths_to_snap_to->back());
430 //if true then this pathvector it_pv is currently being edited in the node tool
432 // char * svgd = sp_svg_write_path(**it_p);
433 // std::cout << "Dumping the pathvector: " << svgd << std::endl;
435 for(Geom::PathVector::iterator it_pv = (*it_p)->begin(); it_pv != (*it_p)->end(); ++it_pv) {
436 // Find a nearest point for each curve within this path
437 // n curves will return n time values with 0 <= t <= 1
438 std::vector<double> anp = (*it_pv).nearestPointPerCurve(p_doc);
440 std::vector<double>::const_iterator np = anp.begin();
441 unsigned int index = 0;
442 for (; np != anp.end(); np++, index++) {
443 Geom::Curve const *curve = &((*it_pv).at_index(index));
444 Geom::Point const sp_doc = curve->pointAt(*np);
446 bool c1 = true;
447 bool c2 = true;
448 if (being_edited) {
449 /* If the path is being edited, then we should only snap though to stationary pieces of the path
450 * and not to the pieces that are being dragged around. This way we avoid
451 * self-snapping. For this we check whether the nodes at both ends of the current
452 * piece are unselected; if they are then this piece must be stationary
453 */
454 g_assert(unselected_nodes != NULL);
455 Geom::Point start_pt = _snapmanager->getDesktop()->doc2dt(curve->pointAt(0));
456 Geom::Point end_pt = _snapmanager->getDesktop()->doc2dt(curve->pointAt(1));
457 c1 = isUnselectedNode(start_pt, unselected_nodes);
458 c2 = isUnselectedNode(end_pt, unselected_nodes);
459 /* Unfortunately, this might yield false positives for coincident nodes. Inkscape might therefore mistakenly
460 * snap to path segments that are not stationary. There are at least two possible ways to overcome this:
461 * - Linking the individual nodes of the SPPath we have here, to the nodes of the NodePath::SubPath class as being
462 * used in sp_nodepath_selected_nodes_move. This class has a member variable called "selected". For this the nodes
463 * should be in the exact same order for both classes, so we can index them
464 * - Replacing the SPPath being used here by the the NodePath::SubPath class; but how?
465 */
466 }
468 Geom::Point const sp_dt = _snapmanager->getDesktop()->doc2dt(sp_doc);
469 if (!being_edited || (c1 && c2)) {
470 Geom::Coord const dist = Geom::distance(sp_doc, p_doc);
471 if (dist < getSnapperTolerance()) {
472 sc.curves.push_back(Inkscape::SnappedCurve(from_2geom(sp_dt), dist, getSnapperTolerance(), getSnapperAlwaysSnap(), false, curve));
473 }
474 }
475 }
476 } // End of: for (Geom::PathVector::iterator ....)
477 }
478 }
480 /* Returns true if point is coincident with one of the unselected nodes */
481 bool Inkscape::ObjectSnapper::isUnselectedNode(Geom::Point const &point, std::vector<Geom::Point> const *unselected_nodes) const
482 {
483 if (unselected_nodes == NULL) {
484 return false;
485 }
487 if (unselected_nodes->size() == 0) {
488 return false;
489 }
491 for (std::vector<Geom::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
492 if (Geom::L2(point - *i) < 1e-4) {
493 return true;
494 }
495 }
497 return false;
498 }
500 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
501 Inkscape::SnapPreferences::PointType const &t,
502 Geom::Point const &p,
503 bool const &first_point,
504 ConstraintLine const &c) const
505 {
507 _collectPaths(t, first_point);
509 // Now we can finally do the real snapping, using the paths collected above
511 g_assert(_snapmanager->getDesktop() != NULL);
512 Geom::Point const p_doc = _snapmanager->getDesktop()->dt2doc(p);
514 Geom::Point direction_vector = c.getDirection();
515 if (!is_zero(direction_vector)) {
516 direction_vector = Geom::unit_vector(direction_vector);
517 }
519 Geom::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
520 Geom::Point const p2_on_cl = p1_on_cl + direction_vector;
522 // The intersection point of the constraint line with any path,
523 // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
524 // The distance between those points is twice the snapping tolerance
525 Geom::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
526 Geom::Point const p_min_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);
527 Geom::Point const p_max_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
529 Geom::Path cl;
530 std::vector<Geom::Path> clv;
531 cl.start(p_min_on_cl);
532 cl.appendNew<Geom::LineSegment>(p_max_on_cl);
533 clv.push_back(cl);
535 for (std::vector<Geom::PathVector*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
536 if (*k) {
537 Geom::CrossingSet cs = Geom::crossings(clv, *(*k));
538 if (cs.size() > 0) {
539 // We need only the first element of cs, because cl is only a single straight linesegment
540 // This first element contains a vector filled with crossings of cl with *k
541 for (std::vector<Geom::Crossing>::const_iterator m = cs[0].begin(); m != cs[0].end(); m++) {
542 if ((*m).ta >= 0 && (*m).ta <= 1 ) {
543 // Reconstruct the point of intersection
544 Geom::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl);
545 // When it's within snapping range, then return it
546 // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1)
547 Geom::Coord dist = Geom::L2(_snapmanager->getDesktop()->dt2doc(p_proj_on_cl) - p_inters);
548 SnappedPoint s(_snapmanager->getDesktop()->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true);
549 sc.points.push_back(s);
550 }
551 }
552 }
553 }
554 }
555 }
558 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
559 Inkscape::SnapPreferences::PointType const &t,
560 Geom::Point const &p,
561 bool const &first_point,
562 Geom::OptRect const &bbox_to_snap,
563 std::vector<SPItem const *> const *it,
564 std::vector<Geom::Point> *unselected_nodes) const
565 {
566 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false ) {
567 return;
568 }
570 /* Get a list of all the SPItems that we will try to snap to */
571 if (first_point) {
572 Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
573 _findCandidates(sp_document_root(_snapmanager->getDocument()), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
574 }
576 if (_snap_to_itemnode || _snap_to_bboxnode || _snap_to_page_border) {
577 _snapNodes(sc, t, p, first_point, unselected_nodes);
578 }
580 if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
581 unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
582 if (n > 0) {
583 /* While editing a path in the node tool, findCandidates must ignore that path because
584 * of the node snapping requirements (i.e. only unselected nodes must be snapable).
585 * That path must not be ignored however when snapping to the paths, so we add it here
586 * manually when applicable
587 */
588 SPPath *path = NULL;
589 if (it != NULL) {
590 g_assert(SP_IS_PATH(*it->begin()));
591 g_assert(it->size() == 1);
592 path = SP_PATH(*it->begin());
593 }
594 _snapPaths(sc, t, p, first_point, unselected_nodes, path);
595 } else {
596 _snapPaths(sc, t, p, first_point, NULL, NULL);
597 }
598 }
599 }
601 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
602 Inkscape::SnapPreferences::PointType const &t,
603 Geom::Point const &p,
604 bool const &first_point,
605 Geom::OptRect const &bbox_to_snap,
606 ConstraintLine const &c,
607 std::vector<SPItem const *> const *it) const
608 {
609 if (_snap_enabled == false || _snapmanager->snapprefs.getSnapFrom(t) == false) {
610 return;
611 }
613 /* Get a list of all the SPItems that we will try to snap to */
614 if (first_point) {
615 Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p);
616 _findCandidates(sp_document_root(_snapmanager->getDocument()), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity());
617 }
619 // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
620 // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
621 // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
623 // When snapping to objects, we either snap to their nodes or their paths. It is however very
624 // unlikely that any node will be exactly at the constrained line, so for a constrained snap
625 // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
626 // so we will more or less snap to them anyhow.
628 if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
629 _snapPathsConstrained(sc, t, p, first_point, c);
630 }
631 }
634 // This method is used to snap a guide to nodes, while dragging the guide around
635 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
636 Geom::Point const &p,
637 Geom::Point const &guide_normal) const
638 {
639 /* Get a list of all the SPItems that we will try to snap to */
640 std::vector<SPItem*> cand;
641 std::vector<SPItem const *> const it; //just an empty list
643 DimensionToSnap snap_dim;
644 if (guide_normal == to_2geom(component_vectors[Geom::Y])) {
645 snap_dim = GUIDE_TRANSL_SNAP_Y;
646 } else if (guide_normal == to_2geom(component_vectors[Geom::X])) {
647 snap_dim = GUIDE_TRANSL_SNAP_X;
648 } else {
649 snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
650 }
652 // We don't support ANGLED_GUIDE_ROT_SNAP yet.
654 // It would be cool to allow the user to rotate a guide by dragging it, instead of
655 // only translating it. (For example when CTRL is pressed). We will need an UI part
656 // for that first; and some important usability choices need to be made:
657 // E.g. which point should be used for pivoting? A previously snapped point,
658 // or a transformation center (which can be moved after clicking for the
659 // second time on an object; but should this point then be constrained to the
660 // line, or can it be located anywhere?)
662 _findCandidates(sp_document_root(_snapmanager->getDocument()), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
663 _snapTranslatingGuideToNodes(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, guide_normal);
664 // _snapRotatingGuideToNodes has not been implemented yet.
665 }
667 /**
668 * \return true if this Snapper will snap at least one kind of point.
669 */
670 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
671 {
672 bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
673 return (_snap_enabled && _snapmanager->snapprefs.getSnapModeBBoxOrNodes() && snap_to_something);
674 }
676 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
677 {
678 bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
679 return (_snap_enabled && _snapmanager->snapprefs.getSnapModeGuide() && snap_to_something);
680 }
682 void Inkscape::ObjectSnapper::_clear_paths() const
683 {
684 for (std::vector<Geom::PathVector*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
685 g_free(*k);
686 }
687 _paths_to_snap_to->clear();
688 }
690 Geom::PathVector* Inkscape::ObjectSnapper::_getBorderPathv() const
691 {
692 Geom::Rect const border_rect = Geom::Rect(Geom::Point(0,0), Geom::Point(sp_document_width(_snapmanager->getDocument()),sp_document_height(_snapmanager->getDocument())));
693 return _getPathvFromRect(border_rect);
694 }
696 Geom::PathVector* Inkscape::ObjectSnapper::_getPathvFromRect(Geom::Rect const rect) const
697 {
698 SPCurve const *border_curve = SPCurve::new_from_rect(rect);
699 if (border_curve) {
700 Geom::PathVector *dummy = new Geom::PathVector(border_curve->get_pathvector());
701 return dummy;
702 } else {
703 return NULL;
704 }
705 }
707 void Inkscape::ObjectSnapper::_getBorderNodes(std::vector<Geom::Point> *points) const
708 {
709 Geom::Coord w = sp_document_width(_snapmanager->getDocument());
710 Geom::Coord h = sp_document_height(_snapmanager->getDocument());
711 points->push_back(Geom::Point(0,0));
712 points->push_back(Geom::Point(0,h));
713 points->push_back(Geom::Point(w,h));
714 points->push_back(Geom::Point(w,0));
715 }
717 /*
718 Local Variables:
719 mode:c++
720 c-file-style:"stroustrup"
721 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
722 indent-tabs-mode:nil
723 fill-column:99
724 End:
725 */
726 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :