]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
release: 1.3.11
[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 = (s1 == SCM_UNDEFINED) ? 0 : gh_scm2int (s1);
34   int h2 = (s2 == SCM_UNDEFINED) ? 0 : gh_scm2int (s2);
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   //  invalidate_cache (Y_AXIS);
120
121   SCM s = get_elt_property ("rests");
122   for (; gh_pair_p (s); s = gh_cdr (s))
123     {
124       Score_element * se = unsmob_element (gh_car (s));
125       Staff_symbol_referencer_interface si (se);
126
127       se->translate_axis (dy_i * si.staff_line_leading_f ()/2.0, Y_AXIS);
128     }
129 }
130
131
132 void
133 Note_column::set_dotcol (Dot_column *d)
134 {
135   add_element (d);
136 }
137
138 /*
139   [TODO]
140   handle rest under beam (do_post: beams are calculated now)
141   what about combination of collisions and rest under beam.
142
143   Should lookup
144     
145     rest -> stem -> beam -> interpolate_y_position ()
146     
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   Direction d = stem_l ()->get_direction ();
161   Real beamy = (stem_l ()->hpos_f () - b->stem(0)->hpos_f ()) * b->slope_f_ + b->left_y_;
162
163   SCM s = get_elt_property ("rests");
164   Score_element * se = unsmob_element (gh_car (s));
165   Staff_symbol_referencer_interface si (se);
166
167   Real staff_space = si.staff_line_leading_f ();      
168   Real rest_dim = extent (Y_AXIS)[d]*2.0  /staff_space ;
169
170   Real minimum_dist
171     = paper_l ()->get_var ("restcollision_minimum_beamdist") ;
172   Real dist =
173     minimum_dist +  -d  * (beamy - rest_dim) >? 0;
174
175   int stafflines = si.lines_i ();
176
177   // move discretely by half spaces.
178   int discrete_dist = int (ceil (dist ));
179
180   // move by whole spaces inside the staff.
181   if (discrete_dist < stafflines+1)
182     discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
183
184   translate_rests (-d *  discrete_dist);
185 }
186
187
188 Interval
189 Note_column::rest_dim () const
190 {
191   Interval restdim;
192   SCM s = get_elt_property ("rests");
193   for (; gh_pair_p (s); s = gh_cdr (s))
194     {
195       Score_element * sc = unsmob_element ( gh_car (s));
196       restdim.unite (sc->extent (Y_AXIS));
197     }
198   
199   return restdim;
200 }
201
202 Note_head*
203 Note_column::first_head () const
204 {
205   Stem * st = stem_l ();
206   return st?  st->first_head (): 0; 
207
208 }