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