]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
patch::: 1.3.14.hwn1
[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--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"
18
19 bool
20 Note_column::rest_b () const
21 {
22   SCM r = get_elt_property ("rests");
23
24   return gh_pair_p (r);
25 }
26
27 int
28 Note_column::shift_compare (Note_column *const &p1, Note_column*const&p2)
29 {
30   SCM s1 = p1->get_elt_property ("horizontal-shift");
31   SCM s2 = p2->get_elt_property ("horizontal-shift");
32
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;
36 }
37
38 Note_column::Note_column()
39 {
40   set_elt_property ("rests", SCM_EOL);
41   set_elt_property ("note-heads", SCM_EOL);  
42   set_axes (X_AXIS, Y_AXIS);
43 }
44
45 Stem *
46 Note_column::stem_l () const
47 {
48   SCM s = get_elt_property ("stem");
49   return dynamic_cast<Stem*> (unsmob_element (s));
50
51 }
52
53   
54 Slice
55 Note_column::head_positions_interval() const
56 {
57   Slice  iv;
58
59   iv.set_empty ();
60
61   SCM h = get_elt_property ("note-heads");
62   for (; gh_pair_p (h); h = gh_cdr (h))
63     {
64       Score_element *se = unsmob_element (gh_car (h));
65       Staff_symbol_referencer_interface si (se); 
66       
67       int j = int (si.position_f ());
68       iv.unite (Slice (j,j));
69     }
70   return iv;
71 }
72
73 Direction
74 Note_column::dir () const
75 {
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 ());
80
81   programming_error ("Note column without heads and stem!");
82   return CENTER;
83 }
84
85
86 void
87 Note_column::set_stem (Stem * stem_l)
88 {
89   set_elt_property ("stem", stem_l->self_scm_);
90
91   add_dependency (stem_l);
92   add_element (stem_l);
93 }
94
95
96
97 void
98 Note_column::add_head (Rhythmic_head *h)
99 {
100   if (Rest*r=dynamic_cast<Rest *> (h))
101     {
102       Group_interface gi (this, "rests");
103       gi.add_element (h);
104     }
105   if (Note_head *nh=dynamic_cast<Note_head *> (h))
106     {
107       Group_interface gi (this, "note-heads");
108       gi.add_element (nh);
109     }
110   add_element (h);
111 }
112
113 /**
114   translate the rest symbols vertically by amount DY_I.
115  */
116 void
117 Note_column::translate_rests (int dy_i)
118 {
119   SCM s = get_elt_property ("rests");
120   for (; gh_pair_p (s); s = gh_cdr (s))
121     {
122       Score_element * se = unsmob_element (gh_car (s));
123       Staff_symbol_referencer_interface si (se);
124
125       se->translate_axis (dy_i * si.staff_space ()/2.0, Y_AXIS);
126     }
127 }
128
129
130 void
131 Note_column::set_dotcol (Dot_column *d)
132 {
133   add_element (d);
134 }
135
136 /*
137   [TODO]
138   handle rest under beam (do_post: beams are calculated now)
139   what about combination of collisions and rest under beam.
140
141   Should lookup
142     
143     rest -> stem -> beam -> interpolate_y_position ()
144     
145 */
146
147 void
148 Note_column::do_post_processing ()
149 {
150   if (!stem_l () || !rest_b ())
151     return;
152
153   Beam * b = stem_l ()->beam_l ();
154   if (!b || !b->stem_count ())
155     return;
156   
157   /* ugh. Should be done by beam.
158      (what? should be done --jcn)
159     scary too?: height is calculated during post_processing
160    */
161   Real beam_dy = 0;
162   Real beam_y = 0;
163
164   SCM s = b->get_elt_property ("height");
165   if (gh_number_p (s))
166     beam_dy = gh_scm2double (s);
167   
168   s = b->get_elt_property ("y-position");
169   if (gh_number_p (s))
170     beam_y = gh_scm2double (s);
171
172   Real x0 = b->first_visible_stem ()->hpos_f ();
173   Real dydx = beam_dy/(b->last_visible_stem ()->hpos_f () - x0);
174
175   Direction d = stem_l ()->get_direction ();
176   Real beamy = (stem_l ()->hpos_f () - x0) * dydx + beam_y;
177
178   s = get_elt_property ("rests");
179   Score_element * se = unsmob_element (gh_car (s));
180   Staff_symbol_referencer_interface si (se);
181
182   Real staff_space = si.staff_space ();      
183   Real rest_dim = extent (Y_AXIS)[d]*2.0  /staff_space ;
184
185   Real minimum_dist
186     = paper_l ()->get_var ("restcollision_minimum_beamdist") ;
187   Real dist =
188     minimum_dist +  -d  * (beamy - rest_dim) >? 0;
189
190   int stafflines = si.lines_i ();
191
192   // move discretely by half spaces.
193   int discrete_dist = int (ceil (dist ));
194
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);
198
199   translate_rests (-d *  discrete_dist);
200 }
201
202
203 Interval
204 Note_column::rest_dim () const
205 {
206   Interval restdim;
207   SCM s = get_elt_property ("rests");
208   for (; gh_pair_p (s); s = gh_cdr (s))
209     {
210       Score_element * sc = unsmob_element ( gh_car (s));
211       restdim.unite (sc->extent (Y_AXIS));
212     }
213   
214   return restdim;
215 }
216
217 Note_head*
218 Note_column::first_head () const
219 {
220   Stem * st = stem_l ();
221   return st?  st->first_head (): 0; 
222 }