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