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"
23 Stem::set_direction (Direction d)
26 warning ("Stem direction set already!");
37 beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
38 yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
45 Stem::head_positions () const
48 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
49 trigger FP exceptions on FreeBSD. Fix: do not return infinity
52 if (!head_l_arr_.size ())
54 return Interval_t<int> (100,-100);
58 for (int i =0; i < head_l_arr_.size (); i++)
60 int p = head_l_arr_[i]->position_i_;
61 r[BIGGER] = r[BIGGER] >? p;
62 r[SMALLER] = r[SMALLER] <? p;
68 Stem::do_print () const
71 DOUT << "flag "<< flag_i_;
78 Stem::stem_length_f () const
80 return yextent_drul_[UP]-yextent_drul_[DOWN] ;
84 Stem::stem_begin_f () const
86 return yextent_drul_[Direction(-dir_)];
90 Stem::chord_start_f () const
92 return head_positions()[dir_] * staff_line_leading_f ()/2.0;
96 Stem::stem_end_f () const
98 return yextent_drul_[dir_];
102 Stem::set_stemend (Real se)
105 if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
106 warning (_ ("weird stem size; check for narrow beams"));
109 yextent_drul_[dir_] = se;
110 yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
114 Stem::type_i () const
116 return head_l_arr_[0]->balltype_i_;
120 Stem::add_head (Rhythmic_head *n)
123 n->add_dependency (this); // ?
124 if (Note_head *nh = dynamic_cast<Note_head *> (n))
126 head_l_arr_.push (nh);
128 else if (Rest *r = dynamic_cast<Rest *> (n))
130 rest_l_arr_.push (r);
135 Stem::invisible_b () const
137 return (!head_l_arr_.size () ||
138 head_l_arr_[0]->balltype_i_ <= 0);
142 Stem::get_center_distance (Direction d) const
144 int staff_center = 0;
145 int distance = d*(head_positions()[d] - staff_center);
146 return distance >? 0;
150 Stem::get_default_dir () const
152 return (get_center_distance (UP) >
153 get_center_distance (DOWN))
159 Stem::get_dir () const
166 Stem::set_default_stemlen ()
169 SCM scm_len = get_elt_property(length_scm_sym);
170 if (scm_len != SCM_BOOL_F)
172 length_f = gh_scm2double (SCM_CDR(scm_len));
175 length_f = paper_l ()->get_var ("stem_length0");
177 bool grace_b = get_elt_property (grace_scm_sym) != SCM_BOOL_F;
178 String type_str = grace_b ? "grace_" : "";
180 Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
183 dir_ = get_default_dir ();
186 stems in unnatural (forced) direction should be shortened,
187 according to [Roush & Gourlay]
189 if (((int)chord_start_f ())
190 && (dir_ != get_default_dir ()))
191 length_f -= shorten_f;
198 set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f:
199 head_positions()[SMALLER] - length_f);
201 if (!grace_b && (dir_ * stem_end_f () < 0))
207 Stem::set_default_extents ()
209 if (!stem_length_f ())
210 set_default_stemlen ();
215 Stem::set_noteheads ()
217 if (!head_l_arr_.size ())
219 head_l_arr_.sort (Note_head::compare);
221 head_l_arr_.reverse ();
223 Note_head * beginhead = head_l_arr_[0];
224 beginhead->set_elt_property (extremal_scm_sym, SCM_BOOL_T);
225 if (beginhead != head_l_arr_.top ())
226 head_l_arr_.top ()->set_elt_property (extremal_scm_sym, SCM_BOOL_T);
229 int lastpos = beginhead->position_i_;
230 for (int i=1; i < head_l_arr_.size (); i ++)
232 int dy =abs (lastpos- head_l_arr_[i]->position_i_);
237 head_l_arr_[i]->flip_around_stem (dir_);
242 lastpos = head_l_arr_[i]->position_i_;
247 Stem::do_pre_processing ()
249 if (yextent_drul_[DOWN]== yextent_drul_[UP])
250 set_default_extents ();
255 set_elt_property (transparent_scm_sym, SCM_BOOL_T);
257 set_empty (invisible_b ());
258 set_spacing_hints ();
264 set stem directions for hinting the optical spacing correction.
266 Modifies DIR_LIST property of the Stem's Score_column
268 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
271 Stem::set_spacing_hints ()
275 SCM scmdir = gh_int2scm (dir_);
276 SCM dirlist = column_l ()->get_elt_property (dir_list_scm_sym);
277 if (dirlist == SCM_BOOL_F)
280 dirlist = SCM_CDR (dirlist);
282 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
284 dirlist = gh_cons (scmdir, dirlist);
285 column_l ()->set_elt_property (dir_list_scm_sym, dirlist);
294 SCM st = get_elt_property (style_scm_sym);
295 if ( st != SCM_BOOL_F)
298 style = ly_scm2string (st);
301 char c = (dir_ == UP) ? 'u' : 'd';
302 return lookup_l ()->afm_find (String ("flags-") + to_str (c) + to_str (flag_i_) + style);
306 Stem::do_width () const
309 if (beam_l_ || abs (flag_i_) <= 2)
313 r = flag ().dim_.x ();
314 r += note_delta_f ();
322 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
325 Stem::do_brew_molecule_p () const
327 Molecule *mol_p =new Molecule;
328 Drul_array<Real> stem_y = yextent_drul_;
329 Real dy = staff_line_leading_f ()/2.0;
332 if (head_l_arr_.size ())
333 head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
334 stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
338 Real stem_width = paper_l ()->get_var ("stemthickness");
339 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
340 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
341 mol_p->add_molecule (ss);
344 if (!beam_l_ && abs (flag_i_) > 2)
346 Molecule fl = flag ();
347 fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
348 mol_p->add_molecule (fl);
351 if (head_l_arr_.size())
353 mol_p->translate_axis (note_delta_f (), X_AXIS);
359 Stem::note_delta_f () const
362 if (head_l_arr_.size())
364 Interval head_wid(0, head_l_arr_[0]->extent (X_AXIS).length ());
365 Real rule_thick(paper_l ()->rule_thickness ());
366 Interval stem_wid(-rule_thick/2, rule_thick/2);
368 r = head_wid.center ();
370 r = head_wid[dir_] - stem_wid[dir_];
376 Stem::hpos_f () const
378 return note_delta_f () + Item::hpos_f ();
382 Stem::do_substitute_element_pointer (Score_element*o,Score_element*n)
384 if (Note_head*h=dynamic_cast<Note_head*> (o))
385 head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
386 if (Rest *r=dynamic_cast<Rest*> (o))
387 rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
388 if (Beam* b = dynamic_cast<Beam*> (o))
391 beam_l_ = dynamic_cast<Beam*> (n);
393 Staff_symbol_referencer::do_substitute_element_pointer (o,n);