]> git.donarmstrong.com Git - lilypond.git/blob - lily/box.cc
Issue 4360: Reorganize smob initialization to make it more reliable
[lilypond.git] / lily / box.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "box.hh"
21
22 ADD_SMOB_INIT (Box);
23
24 void
25 Box::translate (Offset o)
26 {
27   for (Axis i = X_AXIS; i < NO_AXES; incr (i))
28     if (!is_empty (i))
29       interval_a_[i] += o[i];
30 }
31
32 void
33 Box::unite (Box b)
34 {
35   for (Axis i = X_AXIS; i < NO_AXES; incr (i))
36     interval_a_[i].unite (b[i]);
37 }
38
39 Real
40 Box::area () const
41 {
42   return interval_a_[X_AXIS].length ()
43          * interval_a_[Y_AXIS].length ();
44 }
45
46 Box::Box ()
47 {
48 }
49
50 void
51 Box::set_empty ()
52 {
53   interval_a_[X_AXIS].set_empty ();
54   interval_a_[Y_AXIS].set_empty ();
55 }
56
57 bool
58 Box::is_empty () const
59 {
60   return is_empty (X_AXIS) && is_empty (Y_AXIS);
61 }
62
63 bool
64 Box::is_empty (Axis a) const
65 {
66   Interval empty;
67   empty.set_empty ();
68   return interval_a_[a][LEFT] == empty[LEFT]
69     && interval_a_[a][RIGHT] == empty[RIGHT];
70 }
71
72 Box::Box (Interval ix, Interval iy)
73 {
74   x () = ix;
75   y () = iy;
76 }
77
78 Interval &
79 Box::operator [] (Axis a)
80 {
81   return interval_a_[a];
82 }
83
84 Interval
85 Box::operator [] (Axis a) const
86 {
87   return interval_a_[a];
88 }
89
90 void
91 Box::scale (Real s)
92 {
93   interval_a_[X_AXIS] *= s;
94   interval_a_[Y_AXIS] *= s;
95 }
96
97 void
98 Box::add_point (Offset o)
99 {
100   interval_a_[X_AXIS].add_point (o[X_AXIS]);
101   interval_a_[Y_AXIS].add_point (o[Y_AXIS]);
102 }
103
104 Offset
105 Box::center () const
106 {
107   return Offset (interval_a_[X_AXIS].center (),
108                  interval_a_[Y_AXIS].center ());
109 }
110
111 void
112 Box::widen (Real x, Real y)
113 {
114   interval_a_[X_AXIS].widen (x);
115   interval_a_[Y_AXIS].widen (y);
116 }
117
118 void
119 Box::intersect (Box b)
120 {
121   interval_a_[X_AXIS].intersect (b[X_AXIS]);
122   interval_a_[Y_AXIS].intersect (b[Y_AXIS]);
123 }
124
125 // for debugging
126
127 void
128 Box::print ()
129 {
130   printf ("X left %4.4f right %4.4f Y down %4.4f up %4.4f\n",
131           interval_a_[X_AXIS][LEFT], interval_a_[X_AXIS][RIGHT],
132           interval_a_[Y_AXIS][DOWN], interval_a_[Y_AXIS][UP]);
133 }
134
135 /****************************************************************/
136
137
138 const char Box::type_p_name_[] = "ly:box?";