]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
Run `make grand-replace'.
[lilypond.git] / lily / note-column.cc
1 /*
2   note-column.cc -- implement Note_column
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "note-column.hh"
10
11 #include <cmath>                // ceil
12 using namespace std;
13
14 #include "accidental-placement.hh"
15 #include "axis-group-interface.hh"
16 #include "directional-element-interface.hh"
17 #include "international.hh"
18 #include "item.hh"
19 #include "note-head.hh"
20 #include "output-def.hh"
21 #include "pointer-group-interface.hh"
22 #include "rest.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "stem.hh"
25 #include "warn.hh"
26
27 /*
28   TODO: figure out if we can prune this class. This is just an
29   annoying layer between (rest)collision & (note-head + stem)
30 */
31
32 bool
33 Note_column::has_rests (Grob *me)
34 {
35   return unsmob_grob (me->get_object ("rest"));
36 }
37
38 bool
39 Note_column::shift_less (Grob *const &p1, Grob *const &p2)
40 {
41   SCM s1 = p1->get_property ("horizontal-shift");
42   SCM s2 = p2->get_property ("horizontal-shift");
43
44   int h1 = (scm_is_number (s1)) ? scm_to_int (s1) : 0;
45   int h2 = (scm_is_number (s2)) ? scm_to_int (s2) : 0;
46   return h1 < h2;
47 }
48
49 Item *
50 Note_column::get_stem (Grob *me)
51 {
52   SCM s = me->get_object ("stem");
53   return unsmob_item (s);
54 }
55
56 Slice
57 Note_column::head_positions_interval (Grob *me)
58 {
59   Slice iv;
60
61   iv.set_empty ();
62
63   extract_grob_set (me, "note-heads", heads);
64   for (vsize i = 0; i < heads.size (); i++)
65     {
66       Grob *se = heads[i];
67
68       int j = Staff_symbol_referencer::get_rounded_position (se);
69       iv.unite (Slice (j, j));
70     }
71   return iv;
72 }
73
74 Direction
75 Note_column::dir (Grob *me)
76 {
77   Grob *stem = unsmob_grob (me->get_object ("stem"));
78   if (stem && Stem::has_interface (stem))
79     return get_grob_direction (stem);
80   else
81     {
82       extract_grob_set (me, "note-heads", heads);
83       if (heads.size ())
84         return (Direction)sign (head_positions_interval (me).center ());
85     }
86
87   programming_error ("note column without heads and stem");
88   return CENTER;
89 }
90
91 void
92 Note_column::set_stem (Grob *me, Grob *stem)
93 {
94   me->set_object ("stem", stem->self_scm ());
95   Axis_group_interface::add_element (me, stem);
96 }
97
98 Grob *
99 Note_column::get_rest (Grob *me)
100 {
101   return unsmob_grob (me->get_object ("rest"));
102 }
103
104 void
105 Note_column::add_head (Grob *me, Grob *h)
106 {
107   bool both = false;
108   if (Rest::has_interface (h))
109     {
110       extract_grob_set (me, "note-heads", heads);
111       if (heads.size ())
112         both = true;
113       else
114         me->set_object ("rest", h->self_scm ());
115     }
116   else if (Note_head::has_interface (h))
117     {
118       if (unsmob_grob (me->get_object ("rest")))
119         both = true;
120       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
121     }
122
123   if (both)
124     me->warning (_ ("cannot have note heads and rests together on a stem"));
125   else
126     Axis_group_interface::add_element (me, h);
127 }
128
129 /**
130    translate the rest symbols vertically by amount DY, but only if
131    they have no staff-position set.
132 */
133 void
134 Note_column::translate_rests (Grob *me, int dy)
135 {
136   Grob *r = unsmob_grob (me->get_object ("rest"));
137   if (r && !scm_is_number (r->get_property ("staff-position")))
138     {
139       r->translate_axis (dy * Staff_symbol_referencer::staff_space (r) / 2.0, Y_AXIS);
140       Grob *p = r->get_parent (Y_AXIS);
141       p->flush_extent_cache (Y_AXIS);
142     }
143 }
144
145 Grob *
146 Note_column::first_head (Grob *me)
147 {
148   Grob *st = get_stem (me);
149   return st ? Stem::first_head (st) : 0;
150 }
151
152 /*
153   Return the first Accidentals grob that we find in a note-head.
154 */
155 Grob *
156 Note_column::accidentals (Grob *me)
157 {
158   extract_grob_set (me, "note-heads", heads);
159   Grob *acc = 0;
160   for (vsize i = 0; i < heads.size (); i++)
161     {
162       Grob *h = heads[i];
163       acc = h ? unsmob_grob (h->get_object ("accidental-grob")) : 0;
164       if (acc)
165         break;
166     }
167
168   if (!acc)
169     return 0;
170
171   if (Accidental_placement::has_interface (acc->get_parent (X_AXIS)))
172     return acc->get_parent (X_AXIS);
173
174   /* compatibility. */
175   return acc;
176 }
177
178 Grob *
179 Note_column::dot_column (Grob *me)
180 {
181   extract_grob_set (me, "note-heads", heads);
182   for (vsize i = 0; i < heads.size (); i++)
183     {
184       Grob *dots = unsmob_grob (heads[i]->get_object ("dot"));
185       if (dots)
186         return dots->get_parent (X_AXIS);
187     }
188   
189   return 0;
190 }
191
192 Grob *
193 Note_column::arpeggio (Grob *me)
194 {
195   return unsmob_grob (me->get_object ("arpeggio"));
196 }
197
198 /* If a note-column contains a cross-staff stem then
199    nc->extent (Y_AXIS, refp) will not consider the extent of the stem.
200    If you want the extent of the stem to be included (and you are safe
201    from any cross-staff issues) then call this function instead. */
202 Interval
203 Note_column::cross_staff_extent (Grob *me, Grob *refp)
204 {
205   Interval iv = me->extent (refp, Y_AXIS);
206   if (Grob *s = get_stem (me))
207     iv.unite (s->extent (refp, Y_AXIS));
208
209   return iv;
210 }
211
212 ADD_INTERFACE (Note_column,
213                "Stem and noteheads combined.",
214
215                /* properties */
216                "arpeggio "
217                "force-hshift "
218                "horizontal-shift "
219                "ignore-collision "
220                "note-heads "
221                "rest "
222                "rest-collision "
223                "stem "
224                );