2 stem.cc -- implement Stem
4 source file of the GNU LilyPond music typesetter
6 (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
8 TODO: This is way too hairy
14 #include "paper-def.hh"
15 #include "note-head.hh"
17 #include "molecule.hh"
25 IMPLEMENT_IS_TYPE_B1 (Stem,Item);
37 yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
47 Stem::head_positions () const
50 for (int i =0; i < head_l_arr_.size (); i++)
52 int p = head_l_arr_[i]->position_i_;
53 r[BIGGER] = r[BIGGER] >? p;
54 r[SMALLER] = r[SMALLER] <? p;
60 Stem::do_print () const
63 DOUT << "flag "<< flag_i_ << "abbrev_flag_i_" << abbrev_flag_i_;
70 Stem::stem_length_f () const
72 return yextent_drul_[UP]-yextent_drul_[DOWN] ;
76 Stem::stem_begin_f () const
78 return yextent_drul_[Direction(-dir_)];
82 Stem::stem_end_f () const
84 return yextent_drul_[dir_];
88 Stem::set_stemend (Real se)
91 if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
92 warning (_("Weird stem size; check for narrow beams"));
95 yextent_drul_[dir_] = se;
96 yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
100 Stem::type_i () const
102 return head_l_arr_[0]->balltype_i_;
106 Stem::add (Rhythmic_head *n)
108 n->add_dependency (this); // ?
109 if (n->is_type_b (Note_head::static_name ()))
111 head_l_arr_.push ((Note_head*)n);
113 else if (n->is_type_b (Rest::static_name ()))
115 rest_l_arr_.push ((Rest*)n);
120 Stem::invisible_b () const
122 return (!head_l_arr_.size () ||
123 head_l_arr_[0]->balltype_i_ <= 0);
127 Stem::get_center_distance (Direction d)
129 int staff_center = 0;
130 int distance = d*(head_positions()[d] - staff_center);
131 return distance >? 0;
135 Stem::get_default_dir ()
137 return (get_center_distance (UP) >=
138 get_center_distance (DOWN))
150 Stem::set_default_dir ()
152 dir_ = get_default_dir ();
156 Stem::set_default_stemlen ()
159 Real dy = paper ()->interbeam_f ();
161 // ugh, should get nice *rule* for this
162 if (abbrev_flag_i_ > 1)
163 len += (abbrev_flag_i_ - 1)* dy / 2;
169 /* If the stem points in the "wrong" direction, it should be
170 shortened, according to [Roush & Gourlay]. Their suggestion to knock off
171 a whole staffspace is a bit drastical though.
173 else if (dir_ != get_default_dir ())
181 set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + len :
182 head_positions()[SMALLER] - len);
185 if (dir_ * stem_end_f () < 0)
192 Stem::set_default_extents ()
194 if (!stem_length_f ())
195 set_default_stemlen ();
207 move into note_column.cc
211 Stem::set_noteheads ()
213 if (!head_l_arr_.size ())
215 head_l_arr_.sort (Note_head::compare);
217 head_l_arr_.reverse ();
219 head_l_arr_[0]->extremal_i_ = -1;
220 head_l_arr_.top ()->extremal_i_ = 1;
222 int lastpos = head_l_arr_[0]->position_i_;
223 for (int i=1; i < head_l_arr_.size (); i ++)
225 int dy =abs (lastpos- head_l_arr_[i]->position_i_);
230 head_l_arr_[i]->x_dir_ = (stem_xdir_ == LEFT) ? LEFT : RIGHT;
235 lastpos = head_l_arr_[i]->position_i_;
240 Stem::do_pre_processing ()
242 if (yextent_drul_[DOWN]== yextent_drul_[UP])
243 set_default_extents ();
246 transparent_b_ = invisible_b ();
247 set_empty (invisible_b ());
252 Stem::do_width () const
257 r = abbrev_mol ().extent ().x ();
259 else if (beam_l_ || abs (flag_i_) <= 2)
263 Paper_def*p= paper ();
264 r = p->lookup_l ()->flag (flag_i_, dir_).dim_.x ();
265 r += note_delta_f ();
273 Stem::abbrev_mol () const
275 Real dy = paper ()->interbeam_f ();
276 Real w = 1.5 * paper ()->lookup_l ()->ball (2).dim_.x ().length ();
277 Real interline_f = paper ()->interline_f ();
278 Real beamdy = interline_f/2;
281 Real slope_f = paper ()->internote_f () / 4;
285 slope_f = 2 * beam_l_->slope_f_;
286 // ugh, rather calc from Abbreviation_req
287 beams_i = beams_right_i_ >? beams_left_i_;
289 paper ()->lookup_l ()->beam (slope_f, 20 PT, 1 PT);
292 Atom a (paper ()->lookup_l ()->beam (slope_f, w, .48 * interline_f));
293 a.translate (Offset(- w / 2, stem_end_f () - (w / 2 * slope_f)));
297 a.translate_axis (dy + beamdy - dir_ * dy, Y_AXIS);
299 a.translate_axis (2 * beamdy - dir_ * (beamdy - dy), Y_AXIS);
301 for (int i = 0; i < abbrev_flag_i_; i++)
304 b.translate_axis (-dir_ * dy * (beams_i + i), Y_AXIS);
311 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
314 Stem::brew_molecule_p () const
316 Molecule *mol_p =new Molecule;
317 Paper_def *p =paper ();
318 Drul_array<Real> stem_y = yextent_drul_;
319 Real dy = p->internote_f ();
323 if (head_l_arr_.size ())
324 head_wid = head_l_arr_[0]->width ().length ();
325 stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
329 Atom ss =p->lookup_l ()->stem (stem_y[DOWN]*dy,
331 mol_p->add (Atom (ss));
334 if (!beam_l_ &&abs (flag_i_) > 2)
336 Atom fl = p->lookup_l ()->flag (flag_i_, dir_);
337 fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
339 assert (!abbrev_flag_i_);
343 mol_p->add (abbrev_mol ());
345 if (head_l_arr_.size())
347 mol_p->translate_axis (note_delta_f (), X_AXIS);
353 Stem::note_delta_f () const
356 if (head_l_arr_.size() && stem_xdir_ != CENTER)
358 Interval head_wid(0, head_l_arr_[0]->width ().length ());
359 Real rule_thick(paper ()->rule_thickness ());
360 Interval stem_wid(-rule_thick/2, rule_thick/2);
361 r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
366 Stem::hpos_f () const
368 return note_delta_f () +Item::hpos_f ();
372 TODO: head_l_arr_/rest_l_arr_ in do_substitute_dependent ()
375 Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
377 Item * o_l = o->item ();
378 Item * n_l = n? n->item () : 0;
379 head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
380 rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l);