2 stem.cc -- implement Stem
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 TODO: This is way too hairy
13 #include "paper-def.hh"
14 #include "note-head.hh"
16 #include "molecule.hh"
34 beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
37 yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
41 dir_forced_b_ = false;
50 Stem::head_positions () const
53 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
54 trigger FP exceptions on FreeBSD. Fix: do not return infinity
57 if (!head_l_arr_.size ())
59 return Interval_t<int> (100,-100);
63 for (int i =0; i < head_l_arr_.size (); i++)
65 int p = head_l_arr_[i]->position_i_;
66 r[BIGGER] = r[BIGGER] >? p;
67 r[SMALLER] = r[SMALLER] <? p;
73 Stem::do_print () const
76 DOUT << "flag "<< flag_i_;
83 Stem::stem_length_f () const
85 return yextent_drul_[UP]-yextent_drul_[DOWN] ;
89 Stem::stem_begin_f () const
91 return yextent_drul_[Direction(-dir_)];
95 Stem::chord_start_f () const
97 return head_positions()[dir_] * paper ()->internote_f ();
101 Stem::stem_end_f () const
103 return yextent_drul_[dir_];
107 Stem::set_stemend (Real se)
110 if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
111 warning (_ ("weird stem size; check for narrow beams"));
114 yextent_drul_[dir_] = se;
115 yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
119 Stem::type_i () const
121 return head_l_arr_[0]->balltype_i_;
125 Stem::add_head (Rhythmic_head *n)
127 n->add_dependency (this); // ?
128 if (Note_head *nh = dynamic_cast<Note_head *> (n))
130 head_l_arr_.push (nh);
132 else if (Rest *r = dynamic_cast<Rest *> (n))
134 rest_l_arr_.push (r);
139 Stem::invisible_b () const
141 return (!head_l_arr_.size () ||
142 head_l_arr_[0]->balltype_i_ <= 0);
146 Stem::get_center_distance (Direction d) const
148 int staff_center = 0;
149 int distance = d*(head_positions()[d] - staff_center);
150 return distance >? 0;
154 Stem::get_default_dir () const
156 return (get_center_distance (UP) >
157 get_center_distance (DOWN))
163 Stem::get_dir () const
169 Stem::set_default_dir ()
171 dir_ = get_default_dir ();
175 Stem::set_default_stemlen ()
177 Real internote_f = paper ()->internote_f ();
178 Real length_f = paper ()->get_var ("stem_length0") / internote_f;
179 Real shorten_f = paper ()->get_var ("forced_stem_shorten0") / internote_f;
184 stems in unnatural (forced) direction should be shortened,
185 accoding to [Roush & Gourlay]
187 if (((int)chord_start_f ())
188 && (dir_ != get_default_dir ()))
189 length_f -= shorten_f;
196 set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f:
197 head_positions()[SMALLER] - length_f);
199 if (dir_ * stem_end_f () < 0)
205 Stem::set_default_extents ()
207 if (!stem_length_f ())
208 set_default_stemlen ();
220 move into note_column.cc
224 Stem::set_noteheads ()
226 if (!head_l_arr_.size ())
228 head_l_arr_.sort (Note_head::compare);
230 head_l_arr_.reverse ();
232 head_l_arr_[0]->extremal_i_ = -1;
233 head_l_arr_.top ()->extremal_i_ = 1;
235 int lastpos = head_l_arr_[0]->position_i_;
236 for (int i=1; i < head_l_arr_.size (); i ++)
238 int dy =abs (lastpos- head_l_arr_[i]->position_i_);
243 head_l_arr_[i]->x_dir_ = (stem_xdir_ == LEFT) ? LEFT : RIGHT;
248 lastpos = head_l_arr_[i]->position_i_;
253 Stem::do_pre_processing ()
255 if (yextent_drul_[DOWN]== yextent_drul_[UP])
256 set_default_extents ();
259 transparent_b_ = invisible_b ();
260 set_empty (invisible_b ());
265 Stem::do_width () const
268 if (beam_l_ || abs (flag_i_) <= 2)
272 r = lookup_l ()->flag (flag_i_, dir_).dim_.x ();
273 r += note_delta_f ();
281 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
284 Stem::do_brew_molecule_p () const
286 Molecule *mol_p =new Molecule;
287 Drul_array<Real> stem_y = yextent_drul_;
288 Real dy = paper ()->internote_f ();
291 if (head_l_arr_.size ())
292 head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
293 stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
297 Molecule ss =lookup_l ()->stem (stem_y[DOWN]*dy,
299 mol_p->add_molecule (ss);
302 if (!beam_l_ && abs (flag_i_) > 2)
304 Molecule fl = lookup_l ()->flag (flag_i_, dir_);
305 fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
306 mol_p->add_molecule (fl);
309 if (head_l_arr_.size())
311 mol_p->translate_axis (note_delta_f (), X_AXIS);
317 Stem::note_delta_f () const
320 if (head_l_arr_.size())
322 Interval head_wid(0, head_l_arr_[0]->extent (X_AXIS).length ());
323 Real rule_thick(paper ()->rule_thickness ());
324 Interval stem_wid(-rule_thick/2, rule_thick/2);
325 if (stem_xdir_ == CENTER)
326 r = head_wid.center ();
328 r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
334 Stem::hpos_f () const
336 return note_delta_f () + Item::hpos_f ();
340 TODO: head_l_arr_/rest_l_arr_ in
343 Stem::do_substitute_element_pointer (Score_element*o,Score_element*n)
345 if (Note_head*h=dynamic_cast<Note_head*> (o))
346 head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
347 if (Rest *r=dynamic_cast<Rest*> (o))
348 rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
349 if (Beam* b = dynamic_cast<Beam*> (o))
353 beam_l_ = dynamic_cast<Beam*> (n);
356 beams_i_drul_[LEFT] = 0;
357 beams_i_drul_[RIGHT] = 0;