1 /**
2 * \file grid-snapper.cpp
3 * \brief Snapping things to grids.
4 *
5 * Authors:
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * Frank Felfe <innerspace@iname.com>
8 * Carl Hetherington <inkscape@carlh.net>
9 *
10 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
11 * Copyright (C) 1999-2002 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "sp-namedview.h"
17 #include "inkscape.h"
18 #include "desktop.h"
19 #include "display/canvas-grid.h"
20 #include "display/canvas-axonomgrid.h"
22 /**
23 * \return x rounded to the nearest multiple of c1 plus c0.
24 *
25 * \note
26 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
27 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
28 * for guide/grid snapping.
29 */
31 /* FIXME: move this somewhere else, perhaps */
32 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
33 {
34 return floor((x - c0) / c1 + .5) * c1 + c0;
35 }
37 Inkscape::GridSnapper::GridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
38 {
40 }
42 Inkscape::LineSnapper::LineList
43 Inkscape::GridSnapper::_getSnapLines(NR::Point const &p) const
44 {
45 LineList s;
47 if ( NULL == _named_view ) {
48 return s;
49 }
51 CXYGrid *griditem = NULL;
52 for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
53 // FIXME : this is a hack since there is only one view for now
54 // but when we'll handle multiple views, snapping should
55 // must be rethought and maybe only the current view
56 // should give back it's SHOWN lines to snap to
57 // For now, the last CXYGrid in _named_view->gridviews will be used.
58 if ( INKSCAPE_IS_CXYGRID(GTK_OBJECT(l->data)) ) {
59 griditem = INKSCAPE_CXYGRID(l->data);
60 }
61 }
63 g_assert(griditem != NULL);
65 for (unsigned int i = 0; i < 2; ++i) {
67 /* This is to make sure we snap to only visible grid lines */
68 double scaled_spacing = griditem->sw[i]; // this is spacing of visible lines if screen pixels
70 // convert screen pixels to px
71 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
72 if (SP_ACTIVE_DESKTOP) {
73 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
74 }
76 NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
77 scaled_spacing,
78 _named_view->gridorigin[i]);
80 s.push_back(std::make_pair(NR::Dim2(i), rounded));
81 }
83 return s;
84 }
86 /*
87 Local Variables:
88 mode:c++
89 c-file-style:"stroustrup"
90 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
91 indent-tabs-mode:nil
92 fill-column:99
93 End:
94 */
95 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :