1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/**
* \file grid-snapper.cpp
* \brief Snapping things to grids.
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Frank Felfe <innerspace@iname.com>
* Carl Hetherington <inkscape@carlh.net>
*
* Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
* Copyright (C) 1999-2002 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "sp-namedview.h"
#include "inkscape.h"
#include "desktop.h"
#include "display/canvas-grid.h"
#include "display/canvas-axonomgrid.h"
/**
* \return x rounded to the nearest multiple of c1 plus c0.
*
* \note
* If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
* mean "ignore the grid in this dimention". We're currently discussing "good" semantics
* for guide/grid snapping.
*/
/* FIXME: move this somewhere else, perhaps */
static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
{
return floor((x - c0) / c1 + .5) * c1 + c0;
}
Inkscape::GridSnapper::GridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
{
}
Inkscape::LineSnapper::LineList
Inkscape::GridSnapper::_getSnapLines(NR::Point const &p) const
{
LineList s;
if ( NULL == _named_view ) {
return s;
}
SPCGrid *griditem = NULL;
for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
// FIXME : this is a hack since there is only one view for now
// but when we'll handle multiple views, snapping should
// must be rethought and maybe only the current view
// should give back it's SHOWN lines to snap to
// For now, the last SPCGrid in _named_view->gridviews will be used.
if ( SP_IS_CGRID(GTK_OBJECT(l->data)) ) {
griditem = SP_CGRID(l->data);
}
}
g_assert(griditem != NULL);
for (unsigned int i = 0; i < 2; ++i) {
/* This is to make sure we snap to only visible grid lines */
double scaled_spacing = griditem->sw[i]; // this is spacing of visible lines if screen pixels
// convert screen pixels to px
// FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
if (SP_ACTIVE_DESKTOP) {
scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
}
NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
scaled_spacing,
_named_view->gridorigin[i]);
s.push_back(std::make_pair(NR::Dim2(i), rounded));
}
return s;
}
Inkscape::AxonomGridSnapper::AxonomGridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
{
}
Inkscape::LineSnapper::LineList
Inkscape::AxonomGridSnapper::_getSnapLines(NR::Point const &p) const
{
LineList s;
if ( NULL == _named_view ) {
return s;
}
SPCAxonomGrid *griditem = NULL;
for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
// FIXME : this is a hack since there is only one view for now
// but when we'll handle multiple views, snapping should
// must be rethought and maybe only the current view
// should give back it's SHOWN lines to snap to
// For now, the last SPCAxonomGrid in _named_view->gridviews will be used.
if ( SP_IS_CAXONOMGRID(GTK_OBJECT(l->data)) ) {
griditem = SP_CAXONOMGRID(l->data);
}
}
g_assert(griditem != NULL);
// add vertical line.
// This is to make sure we snap to only visible grid lines
double scaled_spacing = griditem->spacing_ylines; // this is spacing of visible lines if screen pixels
// convert screen pixels to px
// FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
if (SP_ACTIVE_DESKTOP) {
scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
}
NR::Coord const rounded = round_to_nearest_multiple_plus(p[0], scaled_spacing, griditem->origin[0]);
int a = round(scaled_spacing);
int b = round(p[0]);
int c = round(rounded);
g_message("hier %d; %d; %d",a,b,c);
s.push_back(std::make_pair(NR::Dim2(0), rounded));
return s;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
|