]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / note-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--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 "note-column.hh"
21
22 #include <cmath>                // ceil
23
24 #include "accidental-placement.hh"
25 #include "axis-group-interface.hh"
26 #include "directional-element-interface.hh"
27 #include "international.hh"
28 #include "item.hh"
29 #include "note-head.hh"
30 #include "output-def.hh"
31 #include "pointer-group-interface.hh"
32 #include "rest.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "stem.hh"
35 #include "warn.hh"
36
37 /*
38   TODO: figure out if we can prune this class. This is just an
39   annoying layer between (rest)collision & (note-head + stem)
40 */
41
42 bool
43 Note_column::has_rests (Grob *me)
44 {
45   return unsmob<Grob> (me->get_object ("rest"));
46 }
47
48 bool
49 Note_column::shift_less (Grob *const &p1, Grob *const &p2)
50 {
51   SCM s1 = p1->get_property ("horizontal-shift");
52   SCM s2 = p2->get_property ("horizontal-shift");
53
54   int h1 = (scm_is_number (s1)) ? scm_to_int (s1) : 0;
55   int h2 = (scm_is_number (s2)) ? scm_to_int (s2) : 0;
56   return h1 < h2;
57 }
58
59 Item *
60 Note_column::get_stem (Grob *me)
61 {
62   SCM s = me->get_object ("stem");
63   return unsmob<Item> (s);
64 }
65
66 Item *
67 Note_column::get_flag (Grob *me)
68 {
69   Item *stem = get_stem (me);
70   if (stem)
71     {
72       SCM s = stem->get_object ("flag");
73       return unsmob<Item> (s);
74     }
75   return 0;
76 }
77
78 Slice
79 Note_column::head_positions_interval (Grob *me)
80 {
81   Slice iv;
82
83   iv.set_empty ();
84
85   extract_grob_set (me, "note-heads", heads);
86   for (vsize i = 0; i < heads.size (); i++)
87     {
88       Grob *se = heads[i];
89
90       int j = Staff_symbol_referencer::get_rounded_position (se);
91       iv.unite (Slice (j, j));
92     }
93   return iv;
94 }
95
96 Direction
97 Note_column::dir (Grob *me)
98 {
99   Grob *stem = unsmob<Grob> (me->get_object ("stem"));
100   if (has_interface<Stem> (stem))
101     return get_grob_direction (stem);
102   else
103     {
104       extract_grob_set (me, "note-heads", heads);
105       if (heads.size ())
106         return (Direction)sign (head_positions_interval (me).center ());
107     }
108
109   if (has_interface<Note_column> (me))
110     programming_error ("Note_column without heads and stem");
111   else
112     programming_error ("dir() given grob without Note_column interface");
113   return CENTER;
114 }
115
116 void
117 Note_column::set_stem (Grob *me, Grob *stem)
118 {
119   me->set_object ("stem", stem->self_scm ());
120   Axis_group_interface::add_element (me, stem);
121 }
122
123 Grob *
124 Note_column::get_rest (Grob *me)
125 {
126   return unsmob<Grob> (me->get_object ("rest"));
127 }
128
129 void
130 Note_column::add_head (Grob *me, Grob *h)
131 {
132   bool both = false;
133   if (has_interface<Rest> (h))
134     {
135       extract_grob_set (me, "note-heads", heads);
136       if (heads.size ())
137         both = true;
138       else
139         me->set_object ("rest", h->self_scm ());
140     }
141   else if (has_interface<Note_head> (h))
142     {
143       if (unsmob<Grob> (me->get_object ("rest")))
144         both = true;
145       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
146     }
147
148   if (both)
149     me->warning (_ ("cannot have note heads and rests together on a stem"));
150   else
151     Axis_group_interface::add_element (me, h);
152 }
153
154 Grob *
155 Note_column::first_head (Grob *me)
156 {
157   Grob *st = get_stem (me);
158   return st ? Stem::first_head (st) : 0;
159 }
160
161 /*
162   Return extent of the noteheads in the "main column",
163   (i.e. excluding any suspended noteheads), or extent
164   of the rest (if there are no heads).
165 */
166 Interval
167 Note_column::calc_main_extent (Grob *me)
168 {
169     Grob *main_head = 0;
170     if (get_stem (me))
171         main_head = first_head (me);
172     else
173       {
174         // no stems => no suspended noteheads.
175         extract_grob_set (me, "note-heads", heads);
176         if (heads.size())
177         main_head = heads[0];
178       }
179     Grob *main_item = main_head
180             ? main_head
181             : unsmob<Grob> (me->get_object ("rest"));
182
183     return main_item
184             ? main_item->extent (me, X_AXIS)
185             : Interval (0, 0);
186 }
187
188 /*
189   Return the first AccidentalPlacement grob that we find in a note-head.
190 */
191 Grob *
192 Note_column::accidentals (Grob *me)
193 {
194   extract_grob_set (me, "note-heads", heads);
195   Grob *acc = 0;
196   for (vsize i = 0; i < heads.size (); i++)
197     {
198       Grob *h = heads[i];
199       acc = h ? unsmob<Grob> (h->get_object ("accidental-grob")) : 0;
200       if (acc)
201         break;
202     }
203
204   if (!acc)
205     return 0;
206
207   if (has_interface<Accidental_placement> (acc->get_parent (X_AXIS)))
208     return acc->get_parent (X_AXIS);
209
210   /* compatibility. */
211   return acc;
212 }
213
214 Grob *
215 Note_column::dot_column (Grob *me)
216 {
217   extract_grob_set (me, "note-heads", heads);
218   for (vsize i = 0; i < heads.size (); i++)
219     {
220       Grob *dots = unsmob<Grob> (heads[i]->get_object ("dot"));
221       if (dots)
222         return dots->get_parent (X_AXIS);
223     }
224
225   return 0;
226 }
227
228 /* If a note-column contains a cross-staff stem then
229    nc->extent (Y_AXIS, refp) will not consider the extent of the stem.
230    If you want the extent of the stem to be included (and you are safe
231    from any cross-staff issues) then call this function instead. */
232 Interval
233 Note_column::cross_staff_extent (Grob *me, Grob *refp)
234 {
235   Interval iv = me->extent (refp, Y_AXIS);
236   if (Grob *s = get_stem (me))
237     iv.unite (s->extent (refp, Y_AXIS));
238
239   return iv;
240 }
241
242 ADD_INTERFACE (Note_column,
243                "Stem and noteheads combined.",
244
245                /* properties */
246                "force-hshift "
247                "horizontal-shift "
248                "ignore-collision "
249                "note-heads "
250                "rest "
251                "rest-collision "
252                "stem "
253               );