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