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
11 #include "directional-element-interface.hh"
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
62 Drul_array<Note_head*> e (extremal_heads ());
64 return Interval (staff_symbol_referencer_interface (e[DOWN]).position_f (),
65 staff_symbol_referencer_interface( e[UP]).position_f ());
70 Stem::chord_start_f () const
72 return head_positions()[get_direction ()]
73 * Staff_symbol_referencer_interface (this).staff_space ()/2.0;
77 Stem::stem_end_position () const
79 SCM p =get_elt_property ("stem-end-position");
83 Stem * me = (Stem*) this;
84 pos = get_default_stem_end_position ();
85 me->set_elt_property ("stem-end-position", gh_double2scm (pos));
88 pos = gh_scm2double (p);
94 Stem::get_direction () const
96 Direction d = directional_element (this).get ();
100 Stem * me = (Stem*) this;
101 d = get_default_dir ();
102 directional_element (me).set (d);
109 Stem::set_stemend (Real se)
112 Direction d= get_direction ();
114 if (d && d * head_positions()[get_direction ()] >= se*d)
115 warning (_ ("Weird stem size; check for narrow beams"));
117 set_elt_property ("stem-end-position", gh_double2scm (se));
121 Stem::type_i () const
123 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);
136 else if (heads_i () == 1)
142 return unsmob_element (gh_car (get_elt_property ("heads")));
145 return first_head ();
150 Stem::heads_i ()const
152 Group_interface gi (this, "heads");
157 The note head which forms one end of the stem.
160 Stem::first_head () const
162 return extremal_heads ()[-get_direction ()];
166 START is part where stem reaches `last' head.
168 Drul_array<Note_head*>
169 Stem::extremal_heads () const
171 const int inf = 1000000;
172 Drul_array<int> extpos;
176 Drul_array<Note_head *> exthead;
177 exthead[LEFT] = exthead[RIGHT] =0;
179 for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
181 Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
182 Staff_symbol_referencer_interface si (n);
184 int p = int(si.position_f ());
188 if (d* p > d* extpos[d])
193 } while (flip (&d) != DOWN);
200 Stem::add_head (Rhythmic_head *n)
202 n->set_elt_property ("stem", this->self_scm_);
203 n->add_dependency (this);
205 Group_interface gi (this);
206 if (Note_head *nh = dynamic_cast<Note_head *> (n))
216 set_elt_property ("heads", SCM_EOL);
217 set_elt_property ("rests", SCM_EOL);
219 dim_cache_[X_AXIS]->off_callbacks_.push ( &Stem::off_callback);
223 Stem::invisible_b () const
226 UGH. Who determines balltype for stem?
228 Note_head * nh = dynamic_cast<Note_head*> (support_head ());
229 return !(heads_i () && nh->balltype_i () >= 1);
233 Stem::get_center_distance (Direction d) const
235 int staff_center = 0;
236 int distance = (int) (d*(head_positions()[d] - staff_center));
237 return distance >? 0;
241 Stem::get_default_dir () const
243 int du = get_center_distance (UP);
244 int dd = get_center_distance (DOWN);
247 return Direction (sign (dd -du));
249 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
253 Stem::get_default_stem_end_position () const
255 bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
256 String type_str = grace_b ? "grace-" : "";
261 SCM scm_len = get_elt_property("length");
262 if (gh_number_p (scm_len))
264 length_f = gh_scm2double (scm_len);
268 s = ly_eval_str (type_str + "stem-length");
269 scm_to_array (s, &a);
270 // stem uses half-spaces
271 length_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
275 s = ly_eval_str (type_str + "stem-shorten");
276 scm_to_array (s, &a);
278 // stem uses half-spaces
280 // fixme: use gh_list_ref () iso. array[]
281 Real shorten_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
284 'set-default-stemlen' sets direction too
286 Direction dir = get_direction ();
289 dir = get_default_dir ();
290 directional_element (this).set (dir);
294 stems in unnatural (forced) direction should be shortened,
295 according to [Roush & Gourlay]
297 if (((int)chord_start_f ())
298 && (get_direction () != get_default_dir ()))
299 length_f -= shorten_f;
302 Real st = head_positions()[dir] + dir * length_f;
304 bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend"));
305 if (!grace_b && !no_extend_b && dir * st < 0)
315 Stem::flag_i () const
317 SCM s = get_elt_property ("duration-log");
318 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
322 Stem::position_noteheads ()
327 Link_array<Score_element> heads =
328 Group_interface__extract_elements (this, (Score_element*)0, "heads");
330 heads.sort (compare_position);
331 Direction dir =get_direction ();
337 Real w = support_head ()->extent (X_AXIS)[dir];
338 for (int i=0; i < heads.size (); i++)
340 heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
343 bool parity= true; // todo: make this settable.
344 int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
345 for (int i=1; i < heads.size (); i ++)
347 Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
348 int dy =abs (lastpos- (int)p);
354 Real l = heads[i]->extent (X_AXIS).length ();
355 heads[i]->translate_axis (l * get_direction (), X_AXIS);
367 Stem::do_pre_processing ()
369 stem_end_position (); // ugh. Trigger direction calc.
370 position_noteheads ();
374 set_elt_property ("transparent", SCM_BOOL_T);
379 set_spacing_hints ();
385 set stem directions for hinting the optical spacing correction.
387 Modifies DIR_LIST property of the Stem's Paper_column
389 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
392 Stem::set_spacing_hints ()
396 SCM scmdir = gh_int2scm (get_direction ());
397 SCM dirlist = column_l ()->get_elt_property ("dir-list");
398 if (dirlist == SCM_UNDEFINED)
401 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
403 dirlist = gh_cons (scmdir, dirlist);
404 column_l ()->set_elt_property ("dir-list", dirlist);
413 SCM st = get_elt_property ("flag-style");
414 if ( st != SCM_UNDEFINED)
416 style = ly_scm2string (st);
419 char c = (get_direction () == UP) ? 'u' : 'd';
420 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
422 if (!style.empty_b ())
423 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
428 Stem::dim_callback (Dimension_cache const* c)
430 Stem * s = dynamic_cast<Stem*> (c->element_l ());
433 if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
437 r = s->flag ().dim_.x ();
443 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
446 Stem::do_brew_molecule_p () const
448 Molecule *mol_p =new Molecule;
450 Staff_symbol_referencer_interface si (first_head ());
452 Real y1 = si.position_f();
453 Real y2 = stem_end_position ();
455 Interval stem_y(y1,y2);
456 stem_y.unite (Interval (y2,y1));
458 Real dy = staff_symbol_referencer_interface (this)
463 head_wid = support_head ()->extent (X_AXIS).length ();
464 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
468 Real stem_width = paper_l ()->get_var ("stemthickness");
469 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
470 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
471 mol_p->add_molecule (ss);
474 if (!beam_l () && abs (flag_i ()) > 2)
476 Molecule fl = flag ();
477 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
478 mol_p->add_molecule (fl);
485 Stem::off_callback (Dimension_cache const * c)
487 Stem * st = dynamic_cast<Stem*> (c->element_l ());
490 if (Note_head * f = st->first_head ())
492 Interval head_wid(0, f->extent (X_AXIS).length ());
494 if (to_boolean (st->get_elt_property ("stem-centered")))
495 return head_wid.center ();
497 Real rule_thick = st->paper_l ()->get_var ("stemthickness");
498 Direction d = st->get_direction ();
499 r = head_wid[d] - d * rule_thick ;
509 SCM b= get_elt_property ("beam");
510 return dynamic_cast<Beam*> (unsmob_element (b));
514 // ugh still very long.
516 Stem::calc_stem_info () const
520 Direction beam_dir = directional_element (beam_l ()).get ();
523 programming_error ("Beam dir not set.");
527 Staff_symbol_referencer_interface st (this);
528 Real staff_space = st.staff_space ();
529 Real half_space = staff_space / 2;
530 Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
531 Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
532 int multiplicity = beam_l ()->get_multiplicity ();
535 info.idealy_f_ = chord_start_f ();
537 // for simplicity, we calculate as if dir == UP
538 info.idealy_f_ *= beam_dir;
539 SCM grace_prop = get_elt_property ("grace");
541 bool grace_b = to_boolean (grace_prop);
545 String type_str = grace_b ? "grace-" : "";
547 s = ly_eval_str (type_str + "beamed-stem-minimum-length");
548 scm_to_array (s, &a);
549 Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
551 s = ly_eval_str (type_str + "beamed-stem-length");
552 scm_to_array (s, &a);
553 Real stem_length = a[multiplicity <? (a.size () - 1)] * staff_space;
555 if (!beam_dir || (beam_dir == directional_element (this).get ()))
556 /* normal beamed stem */
560 info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
562 info.miny_f_ = info.idealy_f_;
563 info.maxy_f_ = INT_MAX;
565 info.idealy_f_ += stem_length;
566 info.miny_f_ += minimum_length;
569 lowest beam of (UP) beam must never be lower than second staffline
571 Hmm, reference (Wanske?)
573 Although this (additional) rule is probably correct,
574 I expect that highest beam (UP) should also never be lower
575 than middle staffline, just as normal stems.
578 bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend"));
579 if (!grace_b && !no_extend_b)
581 /* highest beam of (UP) beam must never be lower than middle
583 lowest beam of (UP) beam must never be lower than second staffline
587 >? (- 2 * half_space - thick
588 + (multiplicity > 0) * thick
589 + interbeam_f * (multiplicity - 1));
595 info.idealy_f_ -= thick;
596 info.maxy_f_ = info.idealy_f_;
597 info.miny_f_ = -INT_MAX;
599 info.idealy_f_ -= stem_length;
600 info.maxy_f_ -= minimum_length;
603 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
605 s = beam_l ()->get_elt_property ("shorten");
607 info.idealy_f_ -= gh_double2scm (s);
609 Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
611 info.idealy_f_ += interstaff_f;
612 info.miny_f_ += interstaff_f;
613 info.maxy_f_ += interstaff_f ;