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>
7 Jan Nieuwenhuizen <janneke@gnu.org>
9 TODO: This is way too hairy
12 #include "dimension-cache.hh"
15 #include "paper-def.hh"
16 #include "note-head.hh"
18 #include "molecule.hh"
19 #include "paper-column.hh"
23 #include "group-interface.hh"
24 #include "cross-staff.hh"
25 #include "staff-symbol-referencer.hh"
29 Stem::set_beaming (int i, Direction d )
31 SCM pair = get_elt_property ("beaming");
33 if (!gh_pair_p (pair))
35 pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
36 set_elt_property ("beaming", pair);
38 index_set_cell (pair, d, gh_int2scm (i));
42 Stem::beam_count (Direction d) const
44 SCM p=get_elt_property ("beaming");
46 return gh_scm2int (index_cell (p,d));
52 Stem::head_positions () const
55 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
56 trigger FP exceptions on FreeBSD. Fix: do not return infinity
61 return Interval_t<int> (100,-100);
64 Link_array<Note_head> head_l_arr =
65 Group_interface__extract_elements (this, (Note_head*)0, "heads");
68 for (int i =0; i < head_l_arr.size (); i++)
70 Staff_symbol_referencer_interface si (head_l_arr[i]);
71 int p = (int)si.position_f ();
72 r[BIGGER] = r[BIGGER] >? p;
73 r[SMALLER] = r[SMALLER] <? p;
80 Stem::chord_start_f () const
82 return head_positions()[get_direction ()]
83 * Staff_symbol_referencer_interface (this).staff_space ()/2.0;
87 Stem::stem_end_position () const
89 SCM p =get_elt_property ("stem-end-position");
93 Stem * me = (Stem*) this;
94 len = get_default_stemlen ();
95 me->set_elt_property ("stem-end-position", gh_double2scm (len));
98 len = gh_scm2double (p);
104 Stem::set_stemend (Real se)
107 Direction d= get_direction ();
109 if (d && d * head_positions()[get_direction ()] >= se*d)
110 warning (_ ("Weird stem size; check for narrow beams"));
112 set_elt_property ("stem-end-position", gh_double2scm (se));
116 Stem::type_i () const
118 return first_head () ? first_head ()->balltype_i () : 2;
124 Note head that determines hshift for upstems
127 Stem::support_head ()const
129 SCM h = get_elt_property ("support-head");
130 Score_element * nh = unsmob_element (h);
134 return first_head ();
139 The note head which forms one end of the stem.
142 Stem::first_head () const
144 const int inf = 1000000;
146 Direction dir = get_direction ();
149 for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
151 Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
152 Staff_symbol_referencer_interface si (n);
153 int p = dir * int(si.position_f ());
165 Stem::add_head (Rhythmic_head *n)
167 n->set_elt_property ("stem", this->self_scm_);
168 n->add_dependency (this); // ?
171 Group_interface gi (this);
172 if (Note_head *nh = dynamic_cast<Note_head *> (n))
182 set_elt_property ("heads", SCM_EOL);
183 set_elt_property ("rests", SCM_EOL);
187 Stem::invisible_b () const
189 return !(first_head () && first_head()->balltype_i () >= 1);
193 Stem::get_center_distance (Direction d) const
195 int staff_center = 0;
196 int distance = d*(head_positions()[d] - staff_center);
197 return distance >? 0;
201 Stem::get_default_dir () const
203 int du = get_center_distance (UP);
204 int dd = get_center_distance (DOWN);
207 return Direction (sign (dd -du));
209 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
213 Stem::get_default_stemlen () const
216 SCM scm_len = get_elt_property("length");
217 if (gh_number_p (scm_len))
219 length_f = gh_scm2double (scm_len);
222 length_f = paper_l ()->get_var ("stem_length0");
224 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
225 String type_str = grace_b ? "grace_" : "";
227 Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
230 'set-default-stemlen' sets direction too
232 Direction dir = get_direction ();
235 Stem * me = (Stem*) this;
236 dir = get_default_dir ();
237 me->set_direction (dir);
241 stems in unnatural (forced) direction should be shortened,
242 according to [Roush & Gourlay]
244 if (((int)chord_start_f ())
245 && (get_direction () != get_default_dir ()))
246 length_f -= shorten_f;
258 Real st = head_positions()[-dir] + dir * length_f;
260 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
261 if (!grace_b && !no_extend_b && dir * st < 0)
268 Stem::flag_i () const
270 SCM s = get_elt_property ("duration-log");
271 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
275 Stem::position_noteheads ()
280 Link_array<Score_element> heads =
281 Group_interface__extract_elements (this, (Score_element*)0, "heads");
283 heads.sort (compare_position);
284 Direction dir =get_direction ();
290 Real w = support_head ()->extent (X_AXIS)[dir];
291 for (int i=0; i < heads.size (); i++)
293 heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
296 bool parity= true; // todo: make this settable.
297 int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
298 for (int i=1; i < heads.size (); i ++)
300 Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
301 int dy =abs (lastpos- (int)p);
307 Real l = heads[i]->extent (X_AXIS).length ();
308 heads[i]->translate_axis (l * get_direction (), X_AXIS);
320 Stem::do_pre_processing ()
322 get_default_stemlen (); // ugh. Trigger direction calc.
323 position_noteheads ();
327 set_elt_property ("transparent", SCM_BOOL_T);
332 set_spacing_hints ();
338 set stem directions for hinting the optical spacing correction.
340 Modifies DIR_LIST property of the Stem's Paper_column
342 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
345 Stem::set_spacing_hints ()
349 SCM scmdir = gh_int2scm (get_direction ());
350 SCM dirlist = column_l ()->get_elt_property ("dir-list");
351 if (dirlist == SCM_UNDEFINED)
354 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
356 dirlist = gh_cons (scmdir, dirlist);
357 column_l ()->set_elt_property ("dir-list", dirlist);
366 SCM st = get_elt_property ("style");
367 if ( st != SCM_UNDEFINED)
369 style = ly_scm2string (st);
372 char c = (get_direction () == UP) ? 'u' : 'd';
373 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
375 if (!style.empty_b ())
376 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
381 Stem::dim_callback (Dimension_cache const* c)
383 Stem * s = dynamic_cast<Stem*> (c->element_l ());
386 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
390 r = s->flag ().dim_.x ();
391 r += s->note_delta_f ();
397 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
400 Stem::do_brew_molecule_p () const
402 Molecule *mol_p =new Molecule;
404 Staff_symbol_referencer_interface si (first_head ());
406 Real y1 = si.position_f();
407 Real y2 = stem_end_position ();
409 Interval stem_y(y1,y2);
410 stem_y.unite (Interval (y2,y1));
412 Real dy = staff_symbol_referencer_interface (this)
417 head_wid = support_head ()->extent (X_AXIS).length ();
418 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
422 Real stem_width = paper_l ()->get_var ("stemthickness");
423 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
424 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
425 mol_p->add_molecule (ss);
428 if (!beam_l () && abs (flag_i ()) > 2)
430 Molecule fl = flag ();
431 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
432 mol_p->add_molecule (fl);
437 mol_p->translate_axis (note_delta_f (), X_AXIS);
443 Stem::note_delta_f () const
448 Interval head_wid(0, first_head()->extent (X_AXIS).length ());
449 Real rule_thick = paper_l ()->get_var ("stemthickness");
451 Interval stem_wid(-rule_thick/2, rule_thick/2);
452 if (get_direction () == CENTER)
453 r = head_wid.center ();
455 r = head_wid[get_direction ()] - stem_wid[get_direction ()];
461 Stem::hpos_f () const
463 return note_delta_f () + Item::hpos_f ();
470 SCM b= get_elt_property ("beam");
471 return dynamic_cast<Beam*> (unsmob_element (b));
475 // ugh still very long.
477 Stem::calc_stem_info () const
481 Direction beam_dir = beam_l ()->get_direction ();
484 programming_error ("Beam dir not set.");
490 = staff_symbol_referencer_interface (this).staff_space ()/2;
491 Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
492 Real beam_f = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
494 info.idealy_f_ = chord_start_f ();
496 // for simplicity, we calculate as if dir == UP
497 info.idealy_f_ *= beam_dir;
498 SCM grace_prop = get_elt_property ("grace");
499 bool grace_b = gh_boolean_p (grace_prop) && gh_scm2bool (grace_prop);
500 SCM extend_prop = get_elt_property ("no-stem-extend");
501 bool no_extend_b = gh_boolean_p (extend_prop) && gh_scm2bool (extend_prop);
503 int stem_max = (int)rint(paper_l ()->get_var ("stem_max"));
504 String type_str = grace_b ? "grace_" : "";
505 Real min_stem_f = paper_l ()->get_var (type_str + "minimum_stem_length"
506 + to_str (beam_l ()->get_multiplicity () <? stem_max)) * internote_f;
507 Real stem_f = paper_l ()->get_var (type_str + "stem_length"
508 + to_str (beam_l ()->get_multiplicity () <? stem_max)) * internote_f;
510 if (!beam_dir || (beam_dir == get_direction ()))
511 /* normal beamed stem */
513 if (beam_l ()->get_multiplicity ())
515 info.idealy_f_ += beam_f
516 + (beam_l ()->get_multiplicity () - 1) * interbeam_f;
518 info.miny_f_ = info.idealy_f_;
519 info.maxy_f_ = INT_MAX;
521 info.idealy_f_ += stem_f;
522 info.miny_f_ += min_stem_f;
525 lowest beam of (UP) beam must never be lower than second staffline
527 Hmm, reference (Wanske?)
529 Although this (additional) rule is probably correct,
530 I expect that highest beam (UP) should also never be lower
531 than middle staffline, just as normal stems.
534 if (!grace_b && !no_extend_b)
536 /* highest beam of (UP) beam must never be lower than middle staffline
538 lowest beam of (UP) beam must never be lower than second staffline
542 >? (- 2 * internote_f - beam_f
543 + (beam_l ()->get_multiplicity () > 0) * beam_f
544 + interbeam_f * (beam_l ()->get_multiplicity () - 1));
550 info.idealy_f_ -= beam_f;
551 info.maxy_f_ = info.idealy_f_;
552 info.miny_f_ = -INT_MAX;
554 info.idealy_f_ -= stem_f;
555 info.maxy_f_ -= min_stem_f;
558 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
560 SCM s = beam_l ()->get_elt_property ("shorten");
562 info.idealy_f_ -= gh_double2scm (s);
564 Real interstaff_f = beam_dir* calc_interstaff_dist (this, beam_l ());
566 info.idealy_f_ += interstaff_f;
567 info.miny_f_ += interstaff_f;
568 info.maxy_f_ += interstaff_f ;