]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
Fix bugs with aligning on main part of the NoteColumn
[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 Grob::unsmob (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 Item::unsmob (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 Item::unsmob (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 = Grob::unsmob (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 Grob::unsmob (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 (Grob::unsmob (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 extent of the noteheads in the "main column",
161   (i.e. excluding any suspended noteheads), or extent
162   of the rest (if there are no heads).
163 */
164 Interval
165 Note_column::calc_main_extent (Grob *me)
166 {
167     Grob *main_head;
168     if (get_stem (me))
169         main_head = first_head (me);
170     else
171       {
172         // no stems => no suspended noteheads.
173         extract_grob_set (me, "note-heads", heads);
174         if (heads.size())
175         main_head = heads[0];
176       }
177     Grob *main_item = main_head
178             ? main_head
179             : Grob::unsmob (me->get_object ("rest"));
180
181     return main_item
182             ? main_item->extent (me, X_AXIS)
183             : Interval (0, 0);
184 }
185
186 /*
187   Return the first AccidentalPlacement grob that we find in a note-head.
188 */
189 Grob *
190 Note_column::accidentals (Grob *me)
191 {
192   extract_grob_set (me, "note-heads", heads);
193   Grob *acc = 0;
194   for (vsize i = 0; i < heads.size (); i++)
195     {
196       Grob *h = heads[i];
197       acc = h ? Grob::unsmob (h->get_object ("accidental-grob")) : 0;
198       if (acc)
199         break;
200     }
201
202   if (!acc)
203     return 0;
204
205   if (Accidental_placement::has_interface (acc->get_parent (X_AXIS)))
206     return acc->get_parent (X_AXIS);
207
208   /* compatibility. */
209   return acc;
210 }
211
212 Grob *
213 Note_column::dot_column (Grob *me)
214 {
215   extract_grob_set (me, "note-heads", heads);
216   for (vsize i = 0; i < heads.size (); i++)
217     {
218       Grob *dots = Grob::unsmob (heads[i]->get_object ("dot"));
219       if (dots)
220         return dots->get_parent (X_AXIS);
221     }
222
223   return 0;
224 }
225
226 /* If a note-column contains a cross-staff stem then
227    nc->extent (Y_AXIS, refp) will not consider the extent of the stem.
228    If you want the extent of the stem to be included (and you are safe
229    from any cross-staff issues) then call this function instead. */
230 Interval
231 Note_column::cross_staff_extent (Grob *me, Grob *refp)
232 {
233   Interval iv = me->extent (refp, Y_AXIS);
234   if (Grob *s = get_stem (me))
235     iv.unite (s->extent (refp, Y_AXIS));
236
237   return iv;
238 }
239
240 ADD_INTERFACE (Note_column,
241                "Stem and noteheads combined.",
242
243                /* properties */
244                "force-hshift "
245                "horizontal-shift "
246                "ignore-collision "
247                "note-heads "
248                "rest "
249                "rest-collision "
250                "stem "
251               );