]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
Merge remote-tracking branch 'origin/translation' into master
[lilypond.git] / lily / note-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 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 Interval
44 Note_column::accidental_width (Grob *me)
45 {
46   extract_grob_set (me, "note-heads", nhs);
47   vector<Grob *> accs;
48   for (vsize i = 0; i < nhs.size (); i++)
49     if (Grob *acc = unsmob_grob (nhs[i]->get_object ("accidental-grob")))
50       accs.push_back (acc);
51
52   Grob *common = common_refpoint_of_array (accs, me, X_AXIS);
53   common = common_refpoint_of_array (nhs, common, X_AXIS);
54
55   Interval nhs_ex = Axis_group_interface::relative_group_extent (nhs, common, X_AXIS);
56   Interval accs_ex = Axis_group_interface::relative_group_extent (accs, common, X_AXIS);
57
58   if (nhs_ex.is_empty ())
59     return accs_ex;
60
61   // want an empty interval here
62   if (accs_ex.is_empty ())
63     return Interval ();
64
65   return Interval (accs_ex[LEFT], nhs_ex[LEFT]);
66 }
67
68 bool
69 Note_column::has_rests (Grob *me)
70 {
71   return unsmob_grob (me->get_object ("rest"));
72 }
73
74 bool
75 Note_column::shift_less (Grob *const &p1, Grob *const &p2)
76 {
77   SCM s1 = p1->get_property ("horizontal-shift");
78   SCM s2 = p2->get_property ("horizontal-shift");
79
80   int h1 = (scm_is_number (s1)) ? scm_to_int (s1) : 0;
81   int h2 = (scm_is_number (s2)) ? scm_to_int (s2) : 0;
82   return h1 < h2;
83 }
84
85 Item *
86 Note_column::get_stem (Grob *me)
87 {
88   SCM s = me->get_object ("stem");
89   return unsmob_item (s);
90 }
91
92 Item *
93 Note_column::get_flag (Grob *me)
94 {
95   Item *stem = get_stem (me);
96   if (stem)
97     {
98       SCM s = stem->get_object ("flag");
99       return unsmob_item (s);
100     }
101   return 0;
102 }
103
104 Slice
105 Note_column::head_positions_interval (Grob *me)
106 {
107   Slice iv;
108
109   iv.set_empty ();
110
111   extract_grob_set (me, "note-heads", heads);
112   for (vsize i = 0; i < heads.size (); i++)
113     {
114       Grob *se = heads[i];
115
116       int j = Staff_symbol_referencer::get_rounded_position (se);
117       iv.unite (Slice (j, j));
118     }
119   return iv;
120 }
121
122 Direction
123 Note_column::dir (Grob *me)
124 {
125   Grob *stem = unsmob_grob (me->get_object ("stem"));
126   if (stem && Stem::has_interface (stem))
127     return get_grob_direction (stem);
128   else
129     {
130       extract_grob_set (me, "note-heads", heads);
131       if (heads.size ())
132         return (Direction)sign (head_positions_interval (me).center ());
133     }
134
135   programming_error ("note column without heads and stem");
136   return CENTER;
137 }
138
139 void
140 Note_column::set_stem (Grob *me, Grob *stem)
141 {
142   me->set_object ("stem", stem->self_scm ());
143   Axis_group_interface::add_element (me, stem);
144 }
145
146 Grob *
147 Note_column::get_rest (Grob *me)
148 {
149   return unsmob_grob (me->get_object ("rest"));
150 }
151
152 void
153 Note_column::add_head (Grob *me, Grob *h)
154 {
155   bool both = false;
156   if (Rest::has_interface (h))
157     {
158       extract_grob_set (me, "note-heads", heads);
159       if (heads.size ())
160         both = true;
161       else
162         me->set_object ("rest", h->self_scm ());
163     }
164   else if (Note_head::has_interface (h))
165     {
166       if (unsmob_grob (me->get_object ("rest")))
167         both = true;
168       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
169     }
170
171   if (both)
172     me->warning (_ ("cannot have note heads and rests together on a stem"));
173   else
174     Axis_group_interface::add_element (me, h);
175 }
176
177 Grob *
178 Note_column::first_head (Grob *me)
179 {
180   Grob *st = get_stem (me);
181   return st ? Stem::first_head (st) : 0;
182 }
183
184 /*
185   Return the first AccidentalPlacement grob that we find in a note-head.
186 */
187 Grob *
188 Note_column::accidentals (Grob *me)
189 {
190   extract_grob_set (me, "note-heads", heads);
191   Grob *acc = 0;
192   for (vsize i = 0; i < heads.size (); i++)
193     {
194       Grob *h = heads[i];
195       acc = h ? unsmob_grob (h->get_object ("accidental-grob")) : 0;
196       if (acc)
197         break;
198     }
199
200   if (!acc)
201     return 0;
202
203   if (Accidental_placement::has_interface (acc->get_parent (X_AXIS)))
204     return acc->get_parent (X_AXIS);
205
206   /* compatibility. */
207   return acc;
208 }
209
210 Grob *
211 Note_column::dot_column (Grob *me)
212 {
213   extract_grob_set (me, "note-heads", heads);
214   for (vsize i = 0; i < heads.size (); i++)
215     {
216       Grob *dots = unsmob_grob (heads[i]->get_object ("dot"));
217       if (dots)
218         return dots->get_parent (X_AXIS);
219     }
220
221   return 0;
222 }
223
224 Grob *
225 Note_column::arpeggio (Grob *me)
226 {
227   return unsmob_grob (me->get_object ("arpeggio"));
228 }
229
230 /* If a note-column contains a cross-staff stem then
231    nc->extent (Y_AXIS, refp) will not consider the extent of the stem.
232    If you want the extent of the stem to be included (and you are safe
233    from any cross-staff issues) then call this function instead. */
234 Interval
235 Note_column::cross_staff_extent (Grob *me, Grob *refp)
236 {
237   Interval iv = me->extent (refp, Y_AXIS);
238   if (Grob *s = get_stem (me))
239     iv.unite (s->extent (refp, Y_AXIS));
240
241   return iv;
242 }
243
244 ADD_INTERFACE (Note_column,
245                "Stem and noteheads combined.",
246
247                /* properties */
248                "arpeggio "
249                "force-hshift "
250                "horizontal-shift "
251                "ignore-collision "
252                "note-heads "
253                "rest "
254                "rest-collision "
255                "stem "
256               );