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"
26 #include "lily-guile.icc"
30 Stem::set_beaming (int i, Direction d )
32 SCM pair = get_elt_property ("beaming");
34 if (!gh_pair_p (pair))
36 pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
37 set_elt_property ("beaming", pair);
39 index_set_cell (pair, d, gh_int2scm (i));
43 Stem::beam_count (Direction d) const
45 SCM p=get_elt_property ("beaming");
47 return gh_scm2int (index_cell (p,d));
53 Stem::head_positions () const
56 Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
57 trigger FP exceptions on FreeBSD. Fix: do not return infinity
62 return Interval_t<int> (100,-100);
65 Link_array<Note_head> head_l_arr =
66 Group_interface__extract_elements (this, (Note_head*)0, "heads");
69 for (int i =0; i < head_l_arr.size (); i++)
71 Staff_symbol_referencer_interface si (head_l_arr[i]);
72 int p = (int)si.position_f ();
73 r[BIGGER] = r[BIGGER] >? p;
74 r[SMALLER] = r[SMALLER] <? p;
81 Stem::chord_start_f () const
83 return head_positions()[get_direction ()]
84 * Staff_symbol_referencer_interface (this).staff_space ()/2.0;
88 Stem::stem_end_position () const
90 SCM p =get_elt_property ("stem-end-position");
94 Stem * me = (Stem*) this;
95 len = get_default_stemlen ();
97 // FIXME: len != position
98 me->set_elt_property ("stem-end-position", gh_double2scm (len));
101 len = gh_scm2double (p);
107 Stem::set_stemend (Real se)
110 Direction d= get_direction ();
112 if (d && d * head_positions()[get_direction ()] >= se*d)
113 warning (_ ("Weird stem size; check for narrow beams"));
115 set_elt_property ("stem-end-position", gh_double2scm (se));
119 Stem::type_i () const
121 return first_head () ? first_head ()->balltype_i () : 2;
127 Note head that determines hshift for upstems
130 Stem::support_head ()const
132 SCM h = get_elt_property ("support-head");
133 Score_element * nh = unsmob_element (h);
137 return first_head ();
142 The note head which forms one end of the stem.
145 Stem::first_head () const
147 const int inf = 1000000;
149 Direction dir = get_direction ();
152 for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
154 Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
155 Staff_symbol_referencer_interface si (n);
156 int p = dir * int(si.position_f ());
168 Stem::add_head (Rhythmic_head *n)
170 n->set_elt_property ("stem", this->self_scm_);
171 n->add_dependency (this); // ?
174 Group_interface gi (this);
175 if (Note_head *nh = dynamic_cast<Note_head *> (n))
185 set_elt_property ("heads", SCM_EOL);
186 set_elt_property ("rests", SCM_EOL);
190 Stem::invisible_b () const
192 return !(first_head () && first_head()->balltype_i () >= 1);
196 Stem::get_center_distance (Direction d) const
198 int staff_center = 0;
199 int distance = d*(head_positions()[d] - staff_center);
200 return distance >? 0;
204 Stem::get_default_dir () const
206 int du = get_center_distance (UP);
207 int dd = get_center_distance (DOWN);
210 return Direction (sign (dd -du));
212 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
216 Stem::get_default_stemlen () const
218 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
219 String type_str = grace_b ? "grace-" : "";
224 SCM scm_len = get_elt_property("length");
225 if (gh_number_p (scm_len))
227 length_f = gh_scm2double (scm_len);
231 s = ly_eval_str (type_str + "stem-length");
232 scm_to_array (s, &a);
233 // stem uses half-spaces
234 length_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
238 s = ly_eval_str (type_str + "stem-shorten");
239 scm_to_array (s, &a);
241 // stem uses half-spaces
242 Real shorten_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
245 'set-default-stemlen' sets direction too
247 Direction dir = get_direction ();
250 Stem * me = (Stem*) this;
251 dir = get_default_dir ();
252 me->set_direction (dir);
256 stems in unnatural (forced) direction should be shortened,
257 according to [Roush & Gourlay]
259 if (((int)chord_start_f ())
260 && (get_direction () != get_default_dir ()))
261 length_f -= shorten_f;
273 Real st_f = head_positions()[-dir] + dir * length_f;
275 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
276 if (!grace_b && !no_extend_b && dir * st_f < 0)
286 Stem::flag_i () const
288 SCM s = get_elt_property ("duration-log");
289 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
293 Stem::position_noteheads ()
298 Link_array<Score_element> heads =
299 Group_interface__extract_elements (this, (Score_element*)0, "heads");
301 heads.sort (compare_position);
302 Direction dir =get_direction ();
308 Real w = support_head ()->extent (X_AXIS)[dir];
309 for (int i=0; i < heads.size (); i++)
311 heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
314 bool parity= true; // todo: make this settable.
315 int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
316 for (int i=1; i < heads.size (); i ++)
318 Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
319 int dy =abs (lastpos- (int)p);
325 Real l = heads[i]->extent (X_AXIS).length ();
326 heads[i]->translate_axis (l * get_direction (), X_AXIS);
338 Stem::do_pre_processing ()
340 get_default_stemlen (); // ugh. Trigger direction calc.
341 position_noteheads ();
345 set_elt_property ("transparent", SCM_BOOL_T);
350 set_spacing_hints ();
356 set stem directions for hinting the optical spacing correction.
358 Modifies DIR_LIST property of the Stem's Paper_column
360 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
363 Stem::set_spacing_hints ()
367 SCM scmdir = gh_int2scm (get_direction ());
368 SCM dirlist = column_l ()->get_elt_property ("dir-list");
369 if (dirlist == SCM_UNDEFINED)
372 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
374 dirlist = gh_cons (scmdir, dirlist);
375 column_l ()->set_elt_property ("dir-list", dirlist);
384 SCM st = get_elt_property ("style");
385 if ( st != SCM_UNDEFINED)
387 style = ly_scm2string (st);
390 char c = (get_direction () == UP) ? 'u' : 'd';
391 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
393 if (!style.empty_b ())
394 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
399 Stem::dim_callback (Dimension_cache const* c)
401 Stem * s = dynamic_cast<Stem*> (c->element_l ());
404 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
408 r = s->flag ().dim_.x ();
409 r += s->note_delta_f ();
415 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
418 Stem::do_brew_molecule_p () const
420 Molecule *mol_p =new Molecule;
422 Staff_symbol_referencer_interface si (first_head ());
424 Real y1 = si.position_f();
425 Real y2 = stem_end_position ();
427 Interval stem_y(y1,y2);
428 stem_y.unite (Interval (y2,y1));
430 Real dy = staff_symbol_referencer_interface (this)
435 head_wid = support_head ()->extent (X_AXIS).length ();
436 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
440 Real stem_width = paper_l ()->get_var ("stemthickness");
441 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
442 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
443 mol_p->add_molecule (ss);
446 if (!beam_l () && abs (flag_i ()) > 2)
448 Molecule fl = flag ();
449 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
450 mol_p->add_molecule (fl);
455 mol_p->translate_axis (note_delta_f (), X_AXIS);
461 Stem::note_delta_f () const
466 Interval head_wid(0, first_head()->extent (X_AXIS).length ());
467 Real rule_thick = paper_l ()->get_var ("stemthickness");
469 Interval stem_wid(-rule_thick/2, rule_thick/2);
470 if (get_direction () == CENTER)
471 r = head_wid.center ();
473 r = head_wid[get_direction ()] - stem_wid[get_direction ()];
479 Stem::hpos_f () const
481 return note_delta_f () + Item::hpos_f ();
488 SCM b= get_elt_property ("beam");
489 return dynamic_cast<Beam*> (unsmob_element (b));
493 // ugh still very long.
495 Stem::calc_stem_info () const
499 Direction beam_dir = beam_l ()->get_direction ();
502 programming_error ("Beam dir not set.");
506 Staff_symbol_referencer_interface st (this);
507 Real staff_space = st.staff_space ();
508 Real half_space = staff_space / 2;
509 Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
510 Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
511 int multiplicity = beam_l ()->get_multiplicity ();
514 info.idealy_f_ = chord_start_f ();
516 // for simplicity, we calculate as if dir == UP
517 info.idealy_f_ *= beam_dir;
518 SCM grace_prop = get_elt_property ("grace");
520 bool grace_b = gh_boolean_p (grace_prop) && gh_scm2bool (grace_prop);
524 String type_str = grace_b ? "grace-" : "";
526 s = ly_eval_str (type_str + "beamed-stem-minimum-length");
527 scm_to_array (s, &a);
528 Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
530 s = ly_eval_str (type_str + "beamed-stem-length");
531 scm_to_array (s, &a);
532 Real stem_length = a[multiplicity <? (a.size () - 1)] * staff_space;
534 if (!beam_dir || (beam_dir == get_direction ()))
535 /* normal beamed stem */
539 info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
541 info.miny_f_ = info.idealy_f_;
542 info.maxy_f_ = INT_MAX;
544 info.idealy_f_ += stem_length;
545 info.miny_f_ += minimum_length;
548 lowest beam of (UP) beam must never be lower than second staffline
550 Hmm, reference (Wanske?)
552 Although this (additional) rule is probably correct,
553 I expect that highest beam (UP) should also never be lower
554 than middle staffline, just as normal stems.
557 SCM extend_prop = get_elt_property ("no-stem-extend");
558 bool no_extend_b = gh_boolean_p (extend_prop)
559 && gh_scm2bool (extend_prop);
560 if (!grace_b && !no_extend_b)
562 /* highest beam of (UP) beam must never be lower than middle
564 lowest beam of (UP) beam must never be lower than second staffline
568 >? (- 2 * half_space - thick
569 + (multiplicity > 0) * thick
570 + interbeam_f * (multiplicity - 1));
576 info.idealy_f_ -= thick;
577 info.maxy_f_ = info.idealy_f_;
578 info.miny_f_ = -INT_MAX;
580 info.idealy_f_ -= stem_length;
581 info.maxy_f_ -= minimum_length;
584 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
586 s = beam_l ()->get_elt_property ("shorten");
588 info.idealy_f_ -= gh_double2scm (s);
590 Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
592 info.idealy_f_ += interstaff_f;
593 info.miny_f_ += interstaff_f;
594 info.maxy_f_ += interstaff_f ;