2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 * move paper vars to scm
19 #include "dimensions.hh"
23 #include "leastsquares.hh"
25 #include "paper-def.hh"
27 #include "group-interface.hh"
28 #include "staff-symbol-referencer.hh"
29 #include "cross-staff.hh"
30 #include "lily-guile.icc"
34 Group_interface g (this, "stems");
39 Beam::add_stem (Stem*s)
41 Group_interface gi (this, "stems");
44 s->add_dependency (this);
46 assert (!s->beam_l ());
47 s->set_elt_property ("beam", self_scm_);
49 if (!spanned_drul_[LEFT])
56 Beam::get_multiplicity () const
59 for (SCM s = get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
61 Score_element * sc = unsmob_element (gh_car (s));
63 if (Stem * st = dynamic_cast<Stem*> (sc))
64 m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
70 After pre-processing all directions should be set.
71 Several post-processing routines (stem, slur, script) need stem/beam
73 Currenly, this means that beam has set all stem's directions.
74 [Alternatively, stems could set its own directions, according to
75 their beam, during 'final-pre-processing'.]
78 Beam::do_pre_processing ()
81 if (visible_stem_count () < 2)
83 warning (_ ("beam has less than two stems"));
84 set_elt_property ("transparent", SCM_BOOL_T);
87 if (!get_direction ())
88 set_direction (get_default_dir ());
91 set_stem_directions ();
100 Beam::get_default_dir () const
102 Drul_array<int> total;
103 total[UP] = total[DOWN] = 0;
104 Drul_array<int> count;
105 count[UP] = count[DOWN] = 0;
108 for (int i=0; i <stem_count (); i++)
109 do { // HUH -- waar slaat dit op?
111 int current = s->get_direction ()
112 ? (1 + d * s->get_direction ())/2
113 : s->get_center_distance ((Direction)-d);
121 } while (flip(&d) != DOWN);
124 SCM s = scm_eval (gh_list (ly_symbol2scm ("beam-dir-algorithm"),
125 ly_quote_scm (gh_cons (gh_int2scm (count[UP]),
126 gh_int2scm (count[DOWN]))),
127 ly_quote_scm (gh_cons (gh_int2scm (total[UP]),
128 gh_int2scm (total[DOWN]))),
130 if (gh_number_p (s) && gh_scm2int (s))
134 If dir is not determined: get from paper
136 return (Direction)(int)
137 paper_l ()->get_var ("stem_default_neutral_direction");
142 Set all stems with non-forced direction to beam direction.
143 Urg: non-forced should become `without/with unforced' direction,
144 once stem gets cleaned-up.
147 Beam::set_stem_directions ()
149 Direction d = get_direction ();
150 for (int i=0; i <stem_count (); i++)
153 SCM force = s->remove_elt_property ("dir-forced");
154 if (!gh_boolean_p (force) || !gh_scm2bool (force))
155 s->set_direction (d);
162 if (!auto_knee ("auto-interstaff-knee-gap", true))
163 auto_knee ("auto-knee-gap", false);
167 Simplistic auto-knees; only consider vertical gap between two
170 `Forced' stem directions are ignored. If you don't want auto-knees,
171 don't set, or unset autoKneeGap/autoInterstaffKneeGap.
174 Beam::auto_knee (String gap_str, bool interstaff_b)
178 SCM gap = get_elt_property (gap_str);
179 if (gh_number_p (gap))
181 int auto_gap_i = gh_scm2int (gap);
182 for (int i=1; i < stem_count (); i++)
184 bool is_b = (bool)(calc_interstaff_dist (stem (i), this)
185 - calc_interstaff_dist (stem (i-1), this));
186 int l_y = (int)(stem (i-1)->head_positions()[get_direction ()])
187 + (int)calc_interstaff_dist (stem (i-1), this);
188 int r_y = (int)(stem (i)->head_positions()[get_direction ()])
189 + (int)calc_interstaff_dist (stem (i), this);
190 int gap_i = r_y - l_y;
192 if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
194 knee_y = (r_y + l_y) / 2;
202 for (int i=0; i < stem_count (); i++)
204 int y = (int)(stem (i)->head_positions()[get_direction ()])
205 + (int)calc_interstaff_dist (stem (i), this);
206 stem (i)->set_direction (y < knee_y ? UP : DOWN);
207 stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
214 Set stem's shorten property if unset.
216 take some y-position (chord/beam/nearest?) into account
217 scmify forced-fraction
220 Beam::set_stem_shorten ()
222 if (!visible_stem_count ())
225 Real forced_fraction = forced_stem_count () / visible_stem_count ();
226 if (forced_fraction < 0.5)
229 int multiplicity = get_multiplicity ();
231 SCM shorten = ly_eval_str ("beamed-stem-shorten");
234 scm_to_array (shorten, &a);
238 Staff_symbol_referencer_interface st (this);
239 Real staff_space = st.staff_space ();
240 Real shorten_f = a[multiplicity <? (a.size () - 1)] * staff_space;
242 /* cute, but who invented this -- how to customise ? */
243 if (forced_fraction < 1)
246 for (int i=0; i < stem_count (); i++)
249 if (s->invisible_b ())
251 if (gh_number_p (s->get_elt_property ("shorten")))
252 s->set_elt_property ("shorten", gh_double2scm (shorten_f));
257 Set elt properties height and y-position if not set.
258 Adjust stem lengths to reach beam.
261 Beam::do_post_processing ()
263 /* first, calculate y, dy */
265 calc_position_and_height (&y, &dy);
266 if (suspect_slope_b (y, dy))
269 Real damped_dy = calc_slope_damping_f (dy);
270 Real quantised_dy = quantise_dy_f (damped_dy);
272 y += (dy - quantised_dy) / 2;
276 until here, we used only stem_info, which acts as if dir=up
278 y *= get_direction ();
279 dy *= get_direction ();
281 /* set or read dy as necessary */
282 SCM s = get_elt_property ("height");
284 dy = gh_scm2double (s);
286 set_elt_property ("height", gh_double2scm (dy));
288 /* set or read y as necessary */
289 s = get_elt_property ("y-position");
292 y = gh_scm2double (s);
293 set_stem_length (y, dy);
297 /* we can modify y, so we should quantise y */
298 Real y_shift = check_stem_length_f (y, dy);
300 y = quantise_y_f (y, dy, 0);
301 set_stem_length (y, dy);
302 y_shift = check_stem_length_f (y, dy);
304 Staff_symbol_referencer_interface st (this);
305 Real half_space = st.staff_space () / 2;
306 if (y_shift > half_space / 4)
311 for significantly lengthened or shortened stems,
312 request quanting the other way.
315 if (abs (y_shift) > half_space / 2)
316 quant_dir = sign (y_shift) * get_direction ();
317 y = quantise_y_f (y, dy, quant_dir);
318 set_stem_length (y, dy);
321 set_elt_property ("y-position", gh_double2scm (y));
326 See Documentation/tex/fonts.doc
329 Beam::calc_position_and_height (Real* y, Real* dy) const
332 if (visible_stem_count () <= 1)
335 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
336 if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
344 Real x0 = first_visible_stem ()->hpos_f ();
345 for (int i=0; i < stem_count (); i++)
348 if (s->invisible_b ())
350 ls.input.push (Offset (s->hpos_f () - x0,
351 s->calc_stem_info ().idealy_f_));
354 ls.minimise (dydx, *y); // duh, takes references
356 Real dx = last_visible_stem ()->hpos_f () - x0;
361 Beam::suspect_slope_b (Real y, Real dy) const
364 steep slope running against lengthened stem is suspect
366 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
367 Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
368 Real lengthened = paper_l ()->get_var ("beam_lengthened");
369 Real steep = paper_l ()->get_var ("beam_steep_slope");
371 Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
374 if (((y - first_ideal > lengthened) && (dydx > steep))
375 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
383 This neat trick is by Werner Lemberg,
384 damped = tanh (slope)
385 corresponds with some tables in [Wanske]
388 Beam::calc_slope_damping_f (Real dy) const
390 SCM damp = get_elt_property ("damping"); // remove?
391 int damping = 1; // ugh.
392 if (gh_number_p (damp))
393 damping = gh_scm2int (damp);
397 Real dx = last_visible_stem ()->hpos_f ()
398 - first_visible_stem ()->hpos_f ();
400 dydx = 0.6 * tanh (dydx) / damping;
407 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
409 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
410 int beam_multiplicity = get_multiplicity ();
411 int stem_multiplicity = (s->flag_i () - 2) >? 0;
413 Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
414 Real x0 = first_visible_stem ()->hpos_f ();
415 Real dx = last_visible_stem ()->hpos_f () - x0;
416 Real stem_y = (s->hpos_f () - x0) / dx * dy + y;
419 if (get_direction () != s->get_direction ())
421 stem_y -= get_direction ()
422 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
424 Staff_symbol_referencer_interface me (s);
425 Staff_symbol_referencer_interface last (last_visible_stem ());
427 // huh, why not for first visible?
428 if (//(s != first_visible_stem ()) &&
429 me.staff_symbol_l () != last.staff_symbol_l ())
430 stem_y += get_direction ()
431 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
437 Beam::check_stem_length_f (Real y, Real dy) const
441 for (int i=0; i < stem_count (); i++)
444 if (s->invisible_b ())
447 Real stem_y = calc_stem_y_f (s, y, dy);
449 stem_y *= get_direction ();
450 Stem_info info = s->calc_stem_info ();
452 if (stem_y > info.maxy_f_)
453 shorten = shorten <? info.maxy_f_ - stem_y;
455 if (stem_y < info.miny_f_)
456 lengthen = lengthen >? info.miny_f_ - stem_y;
459 if (lengthen && shorten)
460 warning (_ ("weird beam vertical offset"));
462 /* when all stems are too short, normal stems win */
464 return shorten * get_direction ();
466 return lengthen * get_direction ();
470 Hmm. At this time, beam position and slope are determined. Maybe,
471 stem directions and length should set to relative to the chord's
472 position of the beam. */
474 Beam::set_stem_length (Real y, Real dy)
476 Staff_symbol_referencer_interface st (this);
477 Real half_space = st.staff_space ()/2;
478 for (int i=0; i < stem_count (); i++)
481 if (s->invisible_b ())
484 Real stem_y = calc_stem_y_f (s, y, dy);
486 /* caution: stem measures in staff-positions */
487 s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
492 [Ross] (simplification of)
493 Set dy complying with:
495 - thick / 2 + staffline_f / 2
496 - thick + staffline_f
500 Beam::quantise_dy_f (Real dy) const
502 SCM quants = ly_eval_str ("beam-height-quants");
505 scm_to_array (quants, &a);
509 Staff_symbol_referencer_interface st (this);
510 Real staff_space = st.staff_space ();
512 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
513 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
517 return q * sign (dy);
521 Prevent interference from stafflines and beams.
522 See Documentation/tex/fonts.doc
524 We only need to quantise the (left) y-position of the beam,
525 since dy is quantised too.
526 if extend_b then stems must *not* get shorter
529 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
531 int multiplicity = get_multiplicity ();
532 Staff_symbol_referencer_interface st (this);
533 Real staff_space = st.staff_space ();
534 SCM quants = scm_eval (gh_list (
535 ly_symbol2scm ("beam-vertical-position-quants"),
536 gh_int2scm (multiplicity),
537 gh_double2scm (dy/staff_space),
540 scm_to_array (quants, &a);
544 Real up_y = get_direction () * y;
545 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
547 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
548 ? iv[SMALLER] : iv[BIGGER];
550 q = iv[(Direction)quant_dir];
552 return q * get_direction ();
556 Beam::set_beaming (Beaming_info_list *beaming)
559 for (int i=0; i < stem_count (); i++)
563 if (stem (i)->beam_count (d) == 0)
564 stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
566 while (flip (&d) != LEFT);
573 beams to go with one stem.
579 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
581 if ((next && !(next->hpos_f () > here->hpos_f ())) ||
582 (prev && !(prev->hpos_f () < here->hpos_f ())))
583 programming_error ("Beams are not left-to-right");
585 Real staffline_f = paper_l ()->get_var ("stafflinethickness");
586 int multiplicity = get_multiplicity ();
589 Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
590 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));;
592 Real dy = interbeam_f;
593 Real stemdx = staffline_f;
595 Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
596 Real dydx = get_real ("height")/dx;
603 if (!here->first_head ())
605 else if (here->type_i ()== 1)
606 nw_f = paper_l ()->get_var ("wholewidth");
607 else if (here->type_i () == 2)
608 nw_f = paper_l ()->get_var ("notewidth") * 0.8;
610 nw_f = paper_l ()->get_var ("quartwidth");
612 /* half beams extending to the left. */
615 int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
616 int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
618 Half beam should be one note-width,
619 but let's make sure two half-beams never touch
621 Real w = here->hpos_f () - prev->hpos_f ();
624 if (lhalfs) // generates warnings if not
625 a = lookup_l ()->beam (dydx, w, thick);
626 a.translate (Offset (-w, -w * dydx));
627 for (int j = 0; j < lhalfs; j++)
630 b.translate_axis (-get_direction () * dy * (lwholebeams+j), Y_AXIS);
631 leftbeams.add_molecule (b);
637 int rhalfs = here->beam_count (RIGHT) - next->beam_count (LEFT);
638 int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
640 Real w = next->hpos_f () - here->hpos_f ();
641 Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
642 a.translate_axis( - stemdx/2, X_AXIS);
646 SCM gap = get_elt_property ("beam-gap");
647 if (gh_number_p (gap))
649 int gap_i = gh_scm2int ( (gap));
650 int nogap = rwholebeams - gap_i;
652 for (; j < nogap; j++)
655 b.translate_axis (-get_direction () * dy * j, Y_AXIS);
656 rightbeams.add_molecule (b);
658 // TODO: notehead widths differ for different types
661 a = lookup_l ()->beam (dydx, w + stemdx, thick);
664 for (; j < rwholebeams; j++)
667 if (!here->invisible_b ())
668 b.translate (Offset (gap_f, -get_direction () * dy * j));
670 b.translate (Offset (0, -get_direction () * dy * j));
671 rightbeams.add_molecule (b);
676 a = lookup_l ()->beam (dydx, w, thick);
678 for (; j < rwholebeams + rhalfs; j++)
681 b.translate_axis (-get_direction () * dy * j, Y_AXIS);
682 rightbeams.add_molecule (b);
686 leftbeams.add_molecule (rightbeams);
689 Does beam quanting think of the asymetry of beams?
690 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
697 Beam::do_brew_molecule_p () const
699 Molecule *mol_p = new Molecule;
703 Real x0 = first_visible_stem ()->hpos_f ();
704 Real dx = last_visible_stem ()->hpos_f () - x0;
705 Real dydx = get_real ("height")/dx;
706 Real y = get_real ("y-position");
707 for (int j=0; j <stem_count (); j++)
710 Stem * prev = (j > 0)? stem (j-1) : 0;
711 Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
713 Molecule sb = stem_beams (i, next, prev);
714 Real x = i->hpos_f ()-x0;
715 sb.translate (Offset (x, x * dydx + y));
716 mol_p->add_molecule (sb);
718 mol_p->translate_axis (x0
719 - spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS), X_AXIS);
725 Beam::forced_stem_count () const
728 for (int i=0; i < stem_count (); i++)
732 if (s->invisible_b ())
735 if (((int)s->chord_start_f ())
736 && (s->get_direction () != s->get_default_dir ()))
745 TODO: Fix this class. This is wildly inefficient.
746 And it sux. Yet another array/list 'interface'.
749 Beam::stem (int i) const
751 return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
755 Beam::stem_count () const
757 Group_interface gi (this, "stems");
762 Beam::stem_top () const
764 SCM s = get_elt_property ("stems");
766 return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
768 //Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[stem_count () - 1];
773 Beam::visible_stem_count () const
776 for (int i = 0; i < stem_count (); i++)
778 if (!stem (i)->invisible_b ())
785 Beam::first_visible_stem () const
787 for (int i = 0; i < stem_count (); i++)
790 if (!s->invisible_b ())
800 Beam::last_visible_stem () const
802 for (int i = stem_count (); i > 0; i--)
804 Stem* s = stem (i - 1);
805 if (!s->invisible_b ())