lilypond-1.1.67
[lilypond.git] / lily / note-column.cc
bloba0fde290c08d79dfaf2c199974b068f57c5db2ff
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"
17 bool
18 Note_column::rest_b () const
20 return rest_l_arr_.size ();
23 int
24 Note_column::shift_compare (Note_column *const &p1, Note_column*const&p2)
26 SCM s1 = p1->get_elt_property (horizontal_shift_scm_sym);
27 SCM s2 = p2->get_elt_property (horizontal_shift_scm_sym);
29 int h1 = (s1 == SCM_BOOL_F) ? 0 : gh_scm2int (SCM_CDR(s1));
30 int h2 = (s2 == SCM_BOOL_F) ? 0 : gh_scm2int (SCM_CDR(s2));
31 return h1 - h2;
34 Note_column::Note_column()
36 set_axes (X_AXIS,X_AXIS);
37 stem_l_ = 0;
40 void
41 Note_column::sort()
43 head_l_arr_.sort (Note_head::compare);
46 Slice
47 Note_column::head_positions_interval() const
49 Slice iv;
51 iv.set_empty ();
52 for (int i=0; i <head_l_arr_.size ();i ++)
54 int j = head_l_arr_[i]->position_i_;
55 iv.unite (Slice (j,j));
57 return iv;
60 Direction
61 Note_column::dir () const
63 if (stem_l_)
64 return stem_l_->dir_;
65 else if (head_l_arr_.size ())
66 return sign (head_positions_interval().center ());
68 programming_error ("Note column without heads and stem!");
69 return CENTER;
73 void
74 Note_column::set_stem (Stem * stem_l)
76 stem_l_ = stem_l;
77 add_dependency (stem_l);
78 add_element (stem_l);
82 void
83 Note_column::do_substitute_element_pointer (Score_element*o, Score_element*n)
85 if (stem_l_ == o)
87 stem_l_ = n ? dynamic_cast<Stem *> (n):0;
89 if (dynamic_cast<Note_head *> (o))
91 head_l_arr_.substitute (dynamic_cast<Note_head *> (o),
92 (n)? dynamic_cast<Note_head *> (n) : 0);
95 if (dynamic_cast<Rest *> (o))
97 rest_l_arr_.substitute (dynamic_cast<Rest *> (o),
98 (n)? dynamic_cast<Rest *> (n) : 0);
102 void
103 Note_column::add_head (Rhythmic_head *h)
105 if (Rest*r=dynamic_cast<Rest *> (h))
107 rest_l_arr_.push (r);
109 if (Note_head *nh=dynamic_cast<Note_head *> (h))
111 head_l_arr_.push (nh);
113 add_element (h);
117 translate the rest symbols vertically by amount DY_I.
119 void
120 Note_column::translate_rests (int dy_i)
122 invalidate_cache (Y_AXIS);
123 for (int i=0; i < rest_l_arr_.size(); i++)
124 rest_l_arr_[i]->position_i_ += dy_i;
127 void
128 Note_column::do_print() const
130 #ifndef NPRINT
131 DOUT << "rests: " << rest_l_arr_.size() << ", ";
132 DOUT << "heads: " << head_l_arr_.size();
133 #endif
136 void
137 Note_column::set_dotcol (Dot_column *d)
139 add_element (d);
143 [TODO]
144 handle rest under beam (do_post: beams are calculated now)
145 what about combination of collisions and rest under beam.
147 Should lookup
149 rest -> stem -> beam -> interpolate_y_position ()
153 void
154 Note_column::do_post_processing ()
156 if (!stem_l_ || !rest_b ())
157 return;
159 Beam * b = stem_l_->beam_l_;
160 if (!b || !b->stems_.size ())
161 return;
163 /* ugh. Should be done by beam. */
164 Direction d = stem_l_->get_dir ();
165 Real beamy = (stem_l_->hpos_f () - b->stems_[0]->hpos_f ()) * b->slope_f_ + b->left_y_;
167 Real staff_space = rest_l_arr_[0]->staff_line_leading_f ();
168 Real rest_dim = extent (Y_AXIS)[d]*2.0 /staff_space ;
170 Real minimum_dist
171 = paper_l ()->get_var ("restcollision_minimum_beamdist") ;
172 Real dist =
173 minimum_dist + -d * (beamy - rest_dim) >? 0;
175 int stafflines = rest_l_arr_[0]->lines_i ();
177 // move discretely by half spaces.
178 int discrete_dist = int (ceil (dist ));
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);
184 translate_rests (-d * discrete_dist);