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
10 #include "dimension-cache.hh"
13 #include "paper-def.hh"
14 #include "note-head.hh"
16 #include "molecule.hh"
17 #include "paper-column.hh"
21 #include "group-interface.hh"
25 beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
26 yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
31 Stem::head_positions () const
34 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
35 trigger FP exceptions on FreeBSD. Fix: do not return infinity
40 return Interval_t<int> (100,-100);
43 Link_array<Note_head> head_l_arr =
44 Group_interface__extract_elements (this, (Note_head*)0, "heads");
47 for (int i =0; i < head_l_arr.size (); i++)
49 int p = (int)head_l_arr[i]->position_f ();
50 r[BIGGER] = r[BIGGER] >? p;
51 r[SMALLER] = r[SMALLER] <? p;
57 Stem::do_print () const
60 DEBUG_OUT << "flag "<< flag_i_;
65 Stem::stem_length_f () const
67 return yextent_drul_[UP]-yextent_drul_[DOWN] ;
71 Stem::stem_begin_f () const
73 return yextent_drul_[Direction(-get_direction ())];
77 Stem::chord_start_f () const
79 return head_positions()[get_direction ()] * staff_line_leading_f ()/2.0;
83 Stem::stem_end_f () const
85 return yextent_drul_[get_direction ()];
89 Stem::set_stemend (Real se)
92 if (get_direction () && get_direction () * head_positions()[get_direction ()] >= se*get_direction ())
93 warning (_ ("Weird stem size; check for narrow beams"));
96 yextent_drul_[get_direction ()] = se;
97 yextent_drul_[Direction(-get_direction ())] = head_positions()[-get_direction ()];
101 Stem::type_i () const
104 return first_head ()->balltype_i_;
108 Stem::first_head () const
110 SCM h =get_elt_property ("heads");
114 Score_element * sc = unsmob_element (gh_car (h));
116 return dynamic_cast<Note_head*> (sc);
120 Stem::add_head (Rhythmic_head *n)
122 n->set_elt_property ("stem", this->self_scm_);
123 n->add_dependency (this); // ?
126 Group_interface gi (this);
127 if (Note_head *nh = dynamic_cast<Note_head *> (n))
136 Stem::invisible_b () const
138 return !(first_head () && first_head()->balltype_i_ >= 1);
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 int du = get_center_distance (UP);
153 int dd = get_center_distance (DOWN);
156 return Direction (sign (dd -du));
158 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
164 Stem::set_default_stemlen ()
167 SCM scm_len = get_elt_property("length");
168 if (scm_len != SCM_UNDEFINED)
170 length_f = gh_scm2double (scm_len);
173 length_f = paper_l ()->get_var ("stem_length0");
175 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
176 String type_str = grace_b ? "grace_" : "";
178 Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
180 if (!get_direction ())
181 set_direction (get_default_dir ());
184 stems in unnatural (forced) direction should be shortened,
185 according to [Roush & Gourlay]
187 if (((int)chord_start_f ())
188 && (get_direction () != get_default_dir ()))
189 length_f -= shorten_f;
196 set_stemend ((get_direction () > 0) ? head_positions()[BIGGER] + length_f:
197 head_positions()[SMALLER] - length_f);
199 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
200 if (!grace_b && !no_extend_b && (get_direction () * stem_end_f () < 0))
206 Stem::set_default_extents ()
208 if (!stem_length_f ())
209 set_default_stemlen ();
214 Stem::set_noteheads ()
220 Link_array<Note_head> head_l_arr =
221 Group_interface__extract_elements (this, (Note_head*)0, "heads");
223 head_l_arr.sort (Note_head::compare);
224 if (get_direction () < 0)
225 head_l_arr.reverse ();
227 Note_head * beginhead = first_head ();
228 beginhead->set_elt_property ("extremal", SCM_BOOL_T);
229 if (beginhead != head_l_arr.top ())
230 head_l_arr.top ()->set_elt_property ("extremal", SCM_BOOL_T);
233 int lastpos = int (beginhead->position_f ());
234 for (int i=1; i < head_l_arr.size (); i ++)
236 int dy =abs (lastpos- (int)head_l_arr[i]->position_f ());
241 head_l_arr[i]->flip_around_stem (get_direction ());
246 lastpos = int (head_l_arr[i]->position_f ());
251 Stem::do_pre_processing ()
253 if (yextent_drul_[DOWN]== yextent_drul_[UP])
254 set_default_extents ();
259 set_elt_property ("transparent", SCM_BOOL_T);
265 set_spacing_hints ();
271 set stem directions for hinting the optical spacing correction.
273 Modifies DIR_LIST property of the Stem's Score_column
275 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
279 Stem::set_spacing_hints ()
283 SCM scmdir = gh_int2scm (get_direction ());
284 SCM dirlist = column_l ()->get_elt_property ("dir-list");
285 if (dirlist == SCM_UNDEFINED)
288 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
290 dirlist = gh_cons (scmdir, dirlist);
291 column_l ()->set_elt_property ("dir-list", dirlist);
300 SCM st = get_elt_property ("style");
301 if ( st != SCM_UNDEFINED)
303 style = ly_scm2string (st);
306 char c = (get_direction () == UP) ? 'u' : 'd';
307 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
309 if (!style.empty_b ())
310 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
315 Stem::dim_callback (Dimension_cache const* c)
317 Stem * s = dynamic_cast<Stem*> (c->element_l ());
320 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i_) <= 2)
324 r = s->flag ().dim_.x ();
325 r += s->note_delta_f ();
333 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
336 Stem::do_brew_molecule_p () const
338 Molecule *mol_p =new Molecule;
339 Drul_array<Real> stem_y = yextent_drul_;
340 Real dy = staff_line_leading_f ()/2.0;
344 head_wid = first_head ()->extent (X_AXIS).length ();
345 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
349 Real stem_width = paper_l ()->get_var ("stemthickness");
350 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
351 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
352 mol_p->add_molecule (ss);
355 if (get_elt_property ("beam") == SCM_UNDEFINED
356 && abs (flag_i_) > 2)
358 Molecule fl = flag ();
359 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
360 mol_p->add_molecule (fl);
365 mol_p->translate_axis (note_delta_f (), X_AXIS);
371 Stem::note_delta_f () const
376 Interval head_wid(0, first_head()->extent (X_AXIS).length ());
377 Real rule_thick = paper_l ()->get_var ("stemthickness");
379 Interval stem_wid(-rule_thick/2, rule_thick/2);
380 if (get_direction () == CENTER)
381 r = head_wid.center ();
383 r = head_wid[get_direction ()] - stem_wid[get_direction ()];
389 Stem::hpos_f () const
391 return note_delta_f () + Item::hpos_f ();
398 SCM b= get_elt_property ("beam");
399 return dynamic_cast<Beam*> (unsmob_element (b));