]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
9da1464728f07e04d3e776b2a12cd77d916af233
[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--2005 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 "axis-group-interface.hh"
15 #include "stem.hh"
16 #include "warn.hh"
17 #include "output-def.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "rest.hh"
20 #include "note-head.hh"
21 #include "accidental-placement.hh"
22 #include "pointer-group-interface.hh"
23 #include "directional-element-interface.hh"
24
25 /*
26   TODO: figure out if we can prune this class. This is just an
27   annoying layer between (rest)collision & (note-head + stem)
28 */
29
30 bool
31 Note_column::has_rests (Grob *me)
32 {
33   return unsmob_grob (me->get_object ("rest"));
34 }
35
36 int
37 Note_column::shift_compare (Grob *const &p1, Grob *const &p2)
38 {
39   SCM s1 = p1->get_property ("horizontal-shift");
40   SCM s2 = p2->get_property ("horizontal-shift");
41
42   int h1 = (scm_is_number (s1)) ? scm_to_int (s1) : 0;
43   int h2 = (scm_is_number (s2)) ? scm_to_int (s2) : 0;
44   return h1 - h2;
45 }
46
47 Item *
48 Note_column::get_stem (Grob *me)
49 {
50   SCM s = me->get_object ("stem");
51   return unsmob_item (s);
52 }
53
54 Slice
55 Note_column::head_positions_interval (Grob *me)
56 {
57   Slice iv;
58
59   iv.set_empty ();
60
61   extract_grob_set (me, "note-heads", heads);
62   for (int i = 0; i < heads.size (); i++)
63     {
64       Grob *se = heads[i];
65
66       int j = Staff_symbol_referencer::get_rounded_position (se);
67       iv.unite (Slice (j, j));
68     }
69   return iv;
70 }
71
72 Direction
73 Note_column::dir (Grob *me)
74 {
75   Grob *stem = unsmob_grob (me->get_object ("stem"));
76   if (stem && Stem::has_interface (stem))
77     return get_grob_direction (stem);
78   else
79     {
80       extract_grob_set (me, "note-heads", heads);
81       if (heads.size ())
82         return (Direction)sign (head_positions_interval (me).center ());
83     }
84
85   programming_error ("note column without heads and stem");
86   return CENTER;
87 }
88
89 void
90 Note_column::set_stem (Grob *me, Grob *stem)
91 {
92   me->set_object ("stem", stem->self_scm ());
93   Axis_group_interface::add_element (me, stem);
94 }
95
96 Grob *
97 Note_column::get_rest (Grob *me)
98 {
99   return unsmob_grob (me->get_object ("rest"));
100 }
101
102 void
103 Note_column::add_head (Grob *me, Grob *h)
104 {
105   bool both = false;
106   if (Rest::has_interface (h))
107     {
108       extract_grob_set (me, "note-heads", heads);
109       if (heads.size ())
110         both = true;
111       else
112         me->set_object ("rest", h->self_scm ());
113     }
114   else if (Note_head::has_interface (h))
115     {
116       if (unsmob_grob (me->get_object ("rest")))
117         both = true;
118       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
119     }
120
121   if (both)
122     me->warning (_ ("can't have note heads and rests together on a stem"));
123   else
124     Axis_group_interface::add_element (me, h);
125 }
126
127 /**
128    translate the rest symbols vertically by amount DY, but only if
129    they have no staff-position set.
130 */
131 void
132 Note_column::translate_rests (Grob *me, int dy)
133 {
134   Grob *r = unsmob_grob (me->get_object ("rest"));
135   if (r && !scm_is_number (r->get_property ("staff-position")))
136     {
137       r->translate_axis (dy * Staff_symbol_referencer::staff_space (r) / 2.0, Y_AXIS);
138       Grob *p = r->get_parent (Y_AXIS);
139       p->flush_extent_cache (Y_AXIS);
140     }
141 }
142
143 void
144 Note_column::set_dotcol (Grob *me, Grob *d)
145 {
146   Axis_group_interface::add_element (me, d);
147 }
148
149 Grob *
150 Note_column::first_head (Grob *me)
151 {
152   Grob *st = get_stem (me);
153   return st ? Stem::first_head (st) : 0;
154 }
155
156 /*
157   Return the first Accidentals grob that we find in a note-head.
158 */
159 Grob *
160 Note_column::accidentals (Grob *me)
161 {
162   extract_grob_set (me, "note-heads", heads);
163   Grob *acc = 0;
164   for (int i = 0; i < heads.size (); i++)
165     {
166       Grob *h = heads[i];
167       acc = h ? unsmob_grob (h->get_object ("accidental-grob")) : 0;
168       if (acc)
169         break;
170     }
171
172   if (!acc)
173     return 0;
174
175   if (Accidental_placement::has_interface (acc->get_parent (X_AXIS)))
176     return acc->get_parent (X_AXIS);
177
178   /* compatibility. */
179   return acc;
180 }
181
182 Grob *
183 Note_column::arpeggio (Grob *me)
184 {
185   return unsmob_grob (me->get_object ("arpeggio"));
186 }
187
188 ADD_INTERFACE (Note_column, "note-column-interface",
189                "Stem and noteheads combined",
190                "arpeggio note-heads rest-collision rest horizontal-shift stem accidentals force-hshift");