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;
263 Real st_f = head_positions()[-dir] + dir * length_f;
265 bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
266 if (!grace_b && !no_extend_b && dir * st_f < 0)
276 Stem::flag_i () const
278 SCM s = get_elt_property ("duration-log");
279 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
283 Stem::position_noteheads ()
288 Link_array<Score_element> heads =
289 Group_interface__extract_elements (this, (Score_element*)0, "heads");
291 heads.sort (compare_position);
292 Direction dir =get_direction ();
298 Real w = support_head ()->extent (X_AXIS)[dir];
299 for (int i=0; i < heads.size (); i++)
301 heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
304 bool parity= true; // todo: make this settable.
305 int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
306 for (int i=1; i < heads.size (); i ++)
308 Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
309 int dy =abs (lastpos- (int)p);
315 Real l = heads[i]->extent (X_AXIS).length ();
316 heads[i]->translate_axis (l * get_direction (), X_AXIS);
328 Stem::do_pre_processing ()
330 get_default_stemlen (); // ugh. Trigger direction calc.
331 position_noteheads ();
335 set_elt_property ("transparent", SCM_BOOL_T);
340 set_spacing_hints ();
346 set stem directions for hinting the optical spacing correction.
348 Modifies DIR_LIST property of the Stem's Paper_column
350 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
353 Stem::set_spacing_hints ()
357 SCM scmdir = gh_int2scm (get_direction ());
358 SCM dirlist = column_l ()->get_elt_property ("dir-list");
359 if (dirlist == SCM_UNDEFINED)
362 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
364 dirlist = gh_cons (scmdir, dirlist);
365 column_l ()->set_elt_property ("dir-list", dirlist);
374 SCM st = get_elt_property ("style");
375 if ( st != SCM_UNDEFINED)
377 style = ly_scm2string (st);
380 char c = (get_direction () == UP) ? 'u' : 'd';
381 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
383 if (!style.empty_b ())
384 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
389 Stem::dim_callback (Dimension_cache const* c)
391 Stem * s = dynamic_cast<Stem*> (c->element_l ());
394 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
398 r = s->flag ().dim_.x ();
399 r += s->note_delta_f ();
405 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
408 Stem::do_brew_molecule_p () const
410 Molecule *mol_p =new Molecule;
412 Staff_symbol_referencer_interface si (first_head ());
414 Real y1 = si.position_f();
415 Real y2 = stem_end_position ();
417 Interval stem_y(y1,y2);
418 stem_y.unite (Interval (y2,y1));
420 Real dy = staff_symbol_referencer_interface (this)
425 head_wid = support_head ()->extent (X_AXIS).length ();
426 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
430 Real stem_width = paper_l ()->get_var ("stemthickness");
431 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
432 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
433 mol_p->add_molecule (ss);
436 if (!beam_l () && abs (flag_i ()) > 2)
438 Molecule fl = flag ();
439 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
440 mol_p->add_molecule (fl);
445 mol_p->translate_axis (note_delta_f (), X_AXIS);
451 Stem::note_delta_f () const
456 Interval head_wid(0, first_head()->extent (X_AXIS).length ());
457 Real rule_thick = paper_l ()->get_var ("stemthickness");
459 Interval stem_wid(-rule_thick/2, rule_thick/2);
460 if (get_direction () == CENTER)
461 r = head_wid.center ();
463 r = head_wid[get_direction ()] - stem_wid[get_direction ()];
469 Stem::hpos_f () const
471 return note_delta_f () + Item::hpos_f ();
478 SCM b= get_elt_property ("beam");
479 return dynamic_cast<Beam*> (unsmob_element (b));
483 // ugh still very long.
485 Stem::calc_stem_info () const
489 Direction beam_dir = beam_l ()->get_direction ();
492 programming_error ("Beam dir not set.");
496 Staff_symbol_referencer_interface st (this);
497 Real staff_space = st.staff_space ();
498 Real half_space = staff_space / 2;
499 Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
500 Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
501 int multiplicity = beam_l ()->get_multiplicity ();
504 info.idealy_f_ = chord_start_f ();
506 // for simplicity, we calculate as if dir == UP
507 info.idealy_f_ *= beam_dir;
508 SCM grace_prop = get_elt_property ("grace");
510 bool grace_b = gh_boolean_p (grace_prop) && gh_scm2bool (grace_prop);
514 String type_str = grace_b ? "grace-" : "";
516 s = ly_eval_str (type_str + "beamed-stem-minimum-length");
517 scm_to_array (s, &a);
518 Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
520 s = ly_eval_str (type_str + "beamed-stem-length");
521 scm_to_array (s, &a);
522 Real stem_length = a[multiplicity <? (a.size () - 1)] * staff_space;
524 if (!beam_dir || (beam_dir == get_direction ()))
525 /* normal beamed stem */
529 info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
531 info.miny_f_ = info.idealy_f_;
532 info.maxy_f_ = INT_MAX;
534 info.idealy_f_ += stem_length;
535 info.miny_f_ += minimum_length;
538 lowest beam of (UP) beam must never be lower than second staffline
540 Hmm, reference (Wanske?)
542 Although this (additional) rule is probably correct,
543 I expect that highest beam (UP) should also never be lower
544 than middle staffline, just as normal stems.
547 SCM extend_prop = get_elt_property ("no-stem-extend");
548 bool no_extend_b = gh_boolean_p (extend_prop)
549 && gh_scm2bool (extend_prop);
550 if (!grace_b && !no_extend_b)
552 /* highest beam of (UP) beam must never be lower than middle
554 lowest beam of (UP) beam must never be lower than second staffline
558 >? (- 2 * half_space - thick
559 + (multiplicity > 0) * thick
560 + interbeam_f * (multiplicity - 1));
566 info.idealy_f_ -= thick;
567 info.maxy_f_ = info.idealy_f_;
568 info.miny_f_ = -INT_MAX;
570 info.idealy_f_ -= stem_length;
571 info.maxy_f_ -= minimum_length;
574 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
576 s = beam_l ()->get_elt_property ("shorten");
578 info.idealy_f_ -= gh_double2scm (s);
580 Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
582 info.idealy_f_ += interstaff_f;
583 info.miny_f_ += interstaff_f;
584 info.maxy_f_ += interstaff_f ;