]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / note-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 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 Slice
68 Note_column::head_positions_interval (Grob *me)
69 {
70   Slice iv;
71
72   iv.set_empty ();
73
74   extract_grob_set (me, "note-heads", heads);
75   for (vsize i = 0; i < heads.size (); i++)
76     {
77       Grob *se = heads[i];
78
79       int j = Staff_symbol_referencer::get_rounded_position (se);
80       iv.unite (Slice (j, j));
81     }
82   return iv;
83 }
84
85 Direction
86 Note_column::dir (Grob *me)
87 {
88   Grob *stem = unsmob_grob (me->get_object ("stem"));
89   if (stem && Stem::has_interface (stem))
90     return get_grob_direction (stem);
91   else
92     {
93       extract_grob_set (me, "note-heads", heads);
94       if (heads.size ())
95         return (Direction)sign (head_positions_interval (me).center ());
96     }
97
98   programming_error ("note column without heads and stem");
99   return CENTER;
100 }
101
102 void
103 Note_column::set_stem (Grob *me, Grob *stem)
104 {
105   me->set_object ("stem", stem->self_scm ());
106   Axis_group_interface::add_element (me, stem);
107 }
108
109 Grob *
110 Note_column::get_rest (Grob *me)
111 {
112   return unsmob_grob (me->get_object ("rest"));
113 }
114
115 void
116 Note_column::add_head (Grob *me, Grob *h)
117 {
118   bool both = false;
119   if (Rest::has_interface (h))
120     {
121       extract_grob_set (me, "note-heads", heads);
122       if (heads.size ())
123         both = true;
124       else
125         me->set_object ("rest", h->self_scm ());
126     }
127   else if (Note_head::has_interface (h))
128     {
129       if (unsmob_grob (me->get_object ("rest")))
130         both = true;
131       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
132     }
133
134   if (both)
135     me->warning (_ ("cannot have note heads and rests together on a stem"));
136   else
137     Axis_group_interface::add_element (me, h);
138 }
139
140 /**
141    translate the rest symbols vertically by amount DY, but only if
142    they have no staff-position set.
143 */
144 void
145 Note_column::translate_rests (Grob *me, int dy)
146 {
147   Grob *r = unsmob_grob (me->get_object ("rest"));
148   if (r && !scm_is_number (r->get_property ("staff-position")))
149     {
150       r->translate_axis (dy * Staff_symbol_referencer::staff_space (r) / 2.0, Y_AXIS);
151       Grob *p = r->get_parent (Y_AXIS);
152       p->flush_extent_cache (Y_AXIS);
153     }
154 }
155
156 Grob *
157 Note_column::first_head (Grob *me)
158 {
159   Grob *st = get_stem (me);
160   return st ? Stem::first_head (st) : 0;
161 }
162
163 /*
164   Return the first Accidentals grob that we find in a note-head.
165 */
166 Grob *
167 Note_column::accidentals (Grob *me)
168 {
169   extract_grob_set (me, "note-heads", heads);
170   Grob *acc = 0;
171   for (vsize i = 0; i < heads.size (); i++)
172     {
173       Grob *h = heads[i];
174       acc = h ? unsmob_grob (h->get_object ("accidental-grob")) : 0;
175       if (acc)
176         break;
177     }
178
179   if (!acc)
180     return 0;
181
182   if (Accidental_placement::has_interface (acc->get_parent (X_AXIS)))
183     return acc->get_parent (X_AXIS);
184
185   /* compatibility. */
186   return acc;
187 }
188
189 Grob *
190 Note_column::dot_column (Grob *me)
191 {
192   extract_grob_set (me, "note-heads", heads);
193   for (vsize i = 0; i < heads.size (); i++)
194     {
195       Grob *dots = unsmob_grob (heads[i]->get_object ("dot"));
196       if (dots)
197         return dots->get_parent (X_AXIS);
198     }
199   
200   return 0;
201 }
202
203 Grob *
204 Note_column::arpeggio (Grob *me)
205 {
206   return unsmob_grob (me->get_object ("arpeggio"));
207 }
208
209 /* If a note-column contains a cross-staff stem then
210    nc->extent (Y_AXIS, refp) will not consider the extent of the stem.
211    If you want the extent of the stem to be included (and you are safe
212    from any cross-staff issues) then call this function instead. */
213 Interval
214 Note_column::cross_staff_extent (Grob *me, Grob *refp)
215 {
216   Interval iv = me->extent (refp, Y_AXIS);
217   if (Grob *s = get_stem (me))
218     iv.unite (s->extent (refp, Y_AXIS));
219
220   return iv;
221 }
222
223 ADD_INTERFACE (Note_column,
224                "Stem and noteheads combined.",
225
226                /* properties */
227                "arpeggio "
228                "force-hshift "
229                "horizontal-shift "
230                "ignore-collision "
231                "note-heads "
232                "rest "
233                "rest-collision "
234                "stem "
235                );