~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to src/grid-snapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-07-06 22:03:02 UTC
  • mto: (2.4.1 sid) (1.4.1 upstream) (45.1.3 maverick)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20060706220302-itgso3qgxdaxjmcy
Tags: upstream-0.44
ImportĀ upstreamĀ versionĀ 0.44

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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) 1999-2002 Authors
 
11
 *
 
12
 * Released under GNU GPL, read the file 'COPYING' for more information
 
13
 */
 
14
 
 
15
#include "sp-namedview.h"
 
16
#include "inkscape.h"
 
17
#include "desktop.h"
 
18
#include "display/canvas-grid.h"
 
19
 
 
20
/**
 
21
 * \return x rounded to the nearest multiple of c1 plus c0.
 
22
 *
 
23
 * \note
 
24
 * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
 
25
 * mean "ignore the grid in this dimention".  We're currently discussing "good" semantics
 
26
 * for guide/grid snapping.
 
27
 */
 
28
 
 
29
/* FIXME: move this somewhere else, perhaps */
 
30
static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
 
31
{
 
32
    return floor((x - c0) / c1 + .5) * c1 + c0;
 
33
}
 
34
 
 
35
Inkscape::GridSnapper::GridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
 
36
{
 
37
 
 
38
}
 
39
 
 
40
Inkscape::LineSnapper::LineList Inkscape::GridSnapper::_getSnapLines(NR::Point const &p) const
 
41
{
 
42
    LineList s;
 
43
 
 
44
    if ( NULL == _named_view ) {
 
45
        return s;
 
46
    }
 
47
 
 
48
    SPCGrid *griditem = NULL;
 
49
    for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
 
50
        // FIXME : this is a hack since there is only one view for now
 
51
        //                 but when we'll handle multiple views, snapping should
 
52
        //                 must be rethought and maybe only the current view
 
53
        //                 should give back it's SHOWN lines to snap to
 
54
        //                 For now, the last SPCGrid in _named_view->gridviews will be used.
 
55
        griditem = SP_CGRID(l->data);
 
56
    }
 
57
 
 
58
    g_assert(griditem != NULL);
 
59
 
 
60
    for (unsigned int i = 0; i < 2; ++i) {
 
61
 
 
62
        /* This is to make sure we snap to only visible grid lines */
 
63
        double scaled_spacing = griditem->sw[i]; // this is spacing of visible lines if screen pixels
 
64
 
 
65
        // convert screen pixels to px
 
66
        // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
 
67
        if (SP_ACTIVE_DESKTOP) {
 
68
            scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
 
69
        }
 
70
 
 
71
        NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
 
72
                                                                 scaled_spacing,
 
73
                                                                 _named_view->gridorigin[i]);
 
74
 
 
75
        s.push_back(std::make_pair(NR::Dim2(i), rounded));
 
76
    }
 
77
 
 
78
    return s;
 
79
}
 
80
 
 
81
/*
 
82
  Local Variables:
 
83
  mode:c++
 
84
  c-file-style:"stroustrup"
 
85
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
86
  indent-tabs-mode:nil
 
87
  fill-column:99
 
88
  End:
 
89
*/
 
90
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :