lilypond-1.3.15
[lilypond.git] / lily / note-column.cc
blob44020d62869f6e092825165806c2c0b3f63ff25f
1 /*
2 note-column.cc -- implement Note_column
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "dot-column.hh"
9 #include "note-column.hh"
10 #include "beam.hh"
11 #include "note-head.hh"
12 #include "stem.hh"
13 #include "rest.hh"
14 #include "debug.hh"
15 #include "paper-def.hh"
16 #include "group-interface.hh"
17 #include "staff-symbol-referencer.hh"
19 bool
20 Note_column::rest_b () const
22 SCM r = get_elt_property ("rests");
24 return gh_pair_p (r);
27 int
28 Note_column::shift_compare (Note_column *const &p1, Note_column*const&p2)
30 SCM s1 = p1->get_elt_property ("horizontal-shift");
31 SCM s2 = p2->get_elt_property ("horizontal-shift");
33 int h1 = (gh_number_p (s1))? gh_scm2int (s1) :0;
34 int h2 = (gh_number_p (s2)) ? gh_scm2int (s2):0;
35 return h1 - h2;
38 Note_column::Note_column()
40 set_elt_property ("rests", SCM_EOL);
41 set_elt_property ("note-heads", SCM_EOL);
42 set_axes (X_AXIS, Y_AXIS);
45 Stem *
46 Note_column::stem_l () const
48 SCM s = get_elt_property ("stem");
49 return dynamic_cast<Stem*> (unsmob_element (s));
54 Slice
55 Note_column::head_positions_interval() const
57 Slice iv;
59 iv.set_empty ();
61 SCM h = get_elt_property ("note-heads");
62 for (; gh_pair_p (h); h = gh_cdr (h))
64 Score_element *se = unsmob_element (gh_car (h));
65 Staff_symbol_referencer_interface si (se);
67 int j = int (si.position_f ());
68 iv.unite (Slice (j,j));
70 return iv;
73 Direction
74 Note_column::dir () const
76 if (stem_l ())
77 return stem_l ()->get_direction ();
78 else if (gh_pair_p (get_elt_property ("note-heads")))
79 return (Direction)sign (head_positions_interval().center ());
81 programming_error ("Note column without heads and stem!");
82 return CENTER;
86 void
87 Note_column::set_stem (Stem * stem_l)
89 set_elt_property ("stem", stem_l->self_scm_);
91 add_dependency (stem_l);
92 add_element (stem_l);
97 void
98 Note_column::add_head (Rhythmic_head *h)
100 if (Rest*r=dynamic_cast<Rest *> (h))
102 Group_interface gi (this, "rests");
103 gi.add_element (h);
105 if (Note_head *nh=dynamic_cast<Note_head *> (h))
107 Group_interface gi (this, "note-heads");
108 gi.add_element (nh);
110 add_element (h);
114 translate the rest symbols vertically by amount DY_I.
116 void
117 Note_column::translate_rests (int dy_i)
119 SCM s = get_elt_property ("rests");
120 for (; gh_pair_p (s); s = gh_cdr (s))
122 Score_element * se = unsmob_element (gh_car (s));
123 Staff_symbol_referencer_interface si (se);
125 se->translate_axis (dy_i * si.staff_space ()/2.0, Y_AXIS);
130 void
131 Note_column::set_dotcol (Dot_column *d)
133 add_element (d);
137 [TODO]
138 handle rest under beam (do_post: beams are calculated now)
139 what about combination of collisions and rest under beam.
141 Should lookup
143 rest -> stem -> beam -> interpolate_y_position ()
147 void
148 Note_column::do_post_processing ()
150 if (!stem_l () || !rest_b ())
151 return;
153 Beam * b = stem_l ()->beam_l ();
154 if (!b || !b->stem_count ())
155 return;
157 /* ugh. Should be done by beam.
158 (what? should be done --jcn)
159 scary too?: height is calculated during post_processing
161 Real beam_dy = 0;
162 Real beam_y = 0;
164 SCM s = b->get_elt_property ("height");
165 if (gh_number_p (s))
166 beam_dy = gh_scm2double (s);
168 s = b->get_elt_property ("y-position");
169 if (gh_number_p (s))
170 beam_y = gh_scm2double (s);
172 Real x0 = b->first_visible_stem ()->hpos_f ();
173 Real dydx = beam_dy/(b->last_visible_stem ()->hpos_f () - x0);
175 Direction d = stem_l ()->get_direction ();
176 Real beamy = (stem_l ()->hpos_f () - x0) * dydx + beam_y;
178 s = get_elt_property ("rests");
179 Score_element * se = unsmob_element (gh_car (s));
180 Staff_symbol_referencer_interface si (se);
182 Real staff_space = si.staff_space ();
183 Real rest_dim = extent (Y_AXIS)[d]*2.0 /staff_space ;
185 Real minimum_dist
186 = paper_l ()->get_var ("restcollision_minimum_beamdist") ;
187 Real dist =
188 minimum_dist + -d * (beamy - rest_dim) >? 0;
190 int stafflines = si.lines_i ();
192 // move discretely by half spaces.
193 int discrete_dist = int (ceil (dist ));
195 // move by whole spaces inside the staff.
196 if (discrete_dist < stafflines+1)
197 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
199 translate_rests (-d * discrete_dist);
203 Interval
204 Note_column::rest_dim () const
206 Interval restdim;
207 SCM s = get_elt_property ("rests");
208 for (; gh_pair_p (s); s = gh_cdr (s))
210 Score_element * sc = unsmob_element ( gh_car (s));
211 restdim.unite (sc->extent (Y_AXIS));
214 return restdim;
217 Note_head*
218 Note_column::first_head () const
220 Stem * st = stem_l ();
221 return st? st->first_head (): 0;