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