]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
Web-ja: update introduction
[lilypond.git] / lily / note-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 (has_interface<Stem> (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   if (has_interface<Note_column> (me))
111     programming_error ("Note_column without heads and stem");
112   else
113     programming_error ("dir() given grob without Note_column interface");
114   return CENTER;
115 }
116
117 void
118 Note_column::set_stem (Grob *me, Grob *stem)
119 {
120   me->set_object ("stem", stem->self_scm ());
121   Axis_group_interface::add_element (me, stem);
122 }
123
124 Grob *
125 Note_column::get_rest (Grob *me)
126 {
127   return unsmob<Grob> (me->get_object ("rest"));
128 }
129
130 void
131 Note_column::add_head (Grob *me, Grob *h)
132 {
133   bool both = false;
134   if (has_interface<Rest> (h))
135     {
136       extract_grob_set (me, "note-heads", heads);
137       if (heads.size ())
138         both = true;
139       else
140         me->set_object ("rest", h->self_scm ());
141     }
142   else if (has_interface<Note_head> (h))
143     {
144       if (unsmob<Grob> (me->get_object ("rest")))
145         both = true;
146       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
147     }
148
149   if (both)
150     me->warning (_ ("cannot have note heads and rests together on a stem"));
151   else
152     Axis_group_interface::add_element (me, h);
153 }
154
155 Grob *
156 Note_column::first_head (Grob *me)
157 {
158   Grob *st = get_stem (me);
159   return st ? Stem::first_head (st) : 0;
160 }
161
162 /*
163   Return extent of the noteheads in the "main column",
164   (i.e. excluding any suspended noteheads), or extent
165   of the rest (if there are no heads).
166 */
167 Interval
168 Note_column::calc_main_extent (Grob *me)
169 {
170     Grob *main_head = 0;
171     if (get_stem (me))
172         main_head = first_head (me);
173     else
174       {
175         // no stems => no suspended noteheads.
176         extract_grob_set (me, "note-heads", heads);
177         if (heads.size())
178         main_head = heads[0];
179       }
180     Grob *main_item = main_head
181             ? main_head
182             : unsmob<Grob> (me->get_object ("rest"));
183
184     return main_item
185             ? main_item->extent (me, X_AXIS)
186             : Interval (0, 0);
187 }
188
189 /*
190   Return the first AccidentalPlacement grob that we find in a note-head.
191 */
192 Grob *
193 Note_column::accidentals (Grob *me)
194 {
195   extract_grob_set (me, "note-heads", heads);
196   Grob *acc = 0;
197   for (vsize i = 0; i < heads.size (); i++)
198     {
199       Grob *h = heads[i];
200       acc = h ? unsmob<Grob> (h->get_object ("accidental-grob")) : 0;
201       if (acc)
202         break;
203     }
204
205   if (!acc)
206     return 0;
207
208   if (has_interface<Accidental_placement> (acc->get_parent (X_AXIS)))
209     return acc->get_parent (X_AXIS);
210
211   /* compatibility. */
212   return acc;
213 }
214
215 Grob *
216 Note_column::dot_column (Grob *me)
217 {
218   extract_grob_set (me, "note-heads", heads);
219   for (vsize i = 0; i < heads.size (); i++)
220     {
221       Grob *dots = unsmob<Grob> (heads[i]->get_object ("dot"));
222       if (dots)
223         return dots->get_parent (X_AXIS);
224     }
225
226   return 0;
227 }
228
229 /* If a note-column contains a cross-staff stem then
230    nc->extent (Y_AXIS, refp) will not consider the extent of the stem.
231    If you want the extent of the stem to be included (and you are safe
232    from any cross-staff issues) then call this function instead. */
233 Interval
234 Note_column::cross_staff_extent (Grob *me, Grob *refp)
235 {
236   Interval iv = me->extent (refp, Y_AXIS);
237   if (Grob *s = get_stem (me))
238     iv.unite (s->extent (refp, Y_AXIS));
239
240   return iv;
241 }
242
243 ADD_INTERFACE (Note_column,
244                "Stem and noteheads combined.",
245
246                /* properties */
247                "force-hshift "
248                "horizontal-shift "
249                "ignore-collision "
250                "note-heads "
251                "rest "
252                "rest-collision "
253                "stem "
254                "glissando-skip "
255               );