2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 * move paper vars to scm
16 remove *-hs variables.
20 #include <math.h> // tanh.
21 #include "directional-element-interface.hh"
23 #include "dimensions.hh"
27 #include "least-squares.hh"
29 #include "paper-def.hh"
31 #include "group-interface.hh"
32 #include "staff-symbol-referencer.hh"
33 #include "cross-staff.hh"
38 Pointer_group_interface g (this, "stems");
41 set_elt_property ("height", gh_int2scm (0)); // ugh.
42 set_elt_property ("y-position" ,gh_int2scm (0));
46 Beam::add_stem (Stem*s)
48 Pointer_group_interface gi (this, "stems");
51 s->add_dependency (this);
53 assert (!s->beam_l ());
54 s->set_elt_pointer ("beam", self_scm_);
56 if (!get_bound (LEFT))
63 Beam::get_multiplicity () const
66 for (SCM s = get_elt_pointer ("stems"); gh_pair_p (s); s = gh_cdr (s))
68 Score_element * sc = unsmob_element (gh_car (s));
70 if (Stem * st = dynamic_cast<Stem*> (sc))
71 m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
77 After pre-processing all directions should be set.
78 Several post-processing routines (stem, slur, script) need stem/beam
80 Currenly, this means that beam has set all stem's directions.
81 [Alternatively, stems could set its own directions, according to
82 their beam, during 'final-pre-processing'.]
85 Beam::before_line_breaking ()
88 if (visible_stem_count () < 2)
90 warning (_ ("beam has less than two stems"));
94 if (!directional_element (this).get ())
95 directional_element (this).set (get_default_dir ());
98 set_stem_directions ();
106 Beam::get_default_dir () const
108 Drul_array<int> total;
109 total[UP] = total[DOWN] = 0;
110 Drul_array<int> count;
111 count[UP] = count[DOWN] = 0;
114 for (int i=0; i <stem_count (); i++)
115 do { // HUH -- waar slaat dit op?
117 Direction sd = directional_element (s).get ();
118 int current = sd ? (1 + d * sd)/2
119 : s->get_center_distance ((Direction)-d);
127 } while (flip(&d) != DOWN);
130 SCM s = scm_eval (gh_list (ly_symbol2scm ("beam-dir-algorithm"),
131 ly_quote_scm (gh_cons (gh_int2scm (count[UP]),
132 gh_int2scm (count[DOWN]))),
133 ly_quote_scm (gh_cons (gh_int2scm (total[UP]),
134 gh_int2scm (total[DOWN]))),
136 if (gh_number_p (s) && gh_scm2int (s))
140 If dir is not determined: get from paper
142 return (Direction)(int)
143 paper_l ()->get_var ("stem_default_neutral_direction");
148 Set all stems with non-forced direction to beam direction.
149 Urg: non-forced should become `without/with unforced' direction,
150 once stem gets cleaned-up.
153 Beam::set_stem_directions ()
155 Direction d = directional_element (this).get ();
156 for (int i=0; i <stem_count (); i++)
159 SCM force = s->remove_elt_property ("dir-forced");
160 if (!gh_boolean_p (force) || !gh_scm2bool (force))
161 directional_element (s).set (d);
168 if (!auto_knee ("auto-interstaff-knee-gap", true))
169 auto_knee ("auto-knee-gap", false);
173 Simplistic auto-knees; only consider vertical gap between two
176 `Forced' stem directions are ignored. If you don't want auto-knees,
177 don't set, or unset autoKneeGap/autoInterstaffKneeGap.
180 Beam::auto_knee (String gap_str, bool interstaff_b)
184 SCM gap = get_elt_property (gap_str);
185 Direction d = directional_element (this).get ();
187 if (gh_number_p (gap))
189 int auto_gap_i = gh_scm2int (gap);
190 for (int i=1; i < stem_count (); i++)
192 bool is_b = (bool)(calc_interstaff_dist (stem (i), this)
193 - calc_interstaff_dist (stem (i-1), this));
194 int l_y = (int)(stem (i-1)->head_positions()[d])
195 + (int)calc_interstaff_dist (stem (i-1), this);
196 int r_y = (int)(stem (i)->head_positions()[d])
197 + (int)calc_interstaff_dist (stem (i), this);
198 int gap_i = r_y - l_y;
200 if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
202 knee_y = (r_y + l_y) / 2;
210 for (int i=0; i < stem_count (); i++)
212 int y = (int)(stem (i)->head_positions()[d])
213 + (int)calc_interstaff_dist (stem (i), this);
214 directional_element (stem (i)).set (y < knee_y ? UP : DOWN);
215 stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
222 Set stem's shorten property if unset.
224 take some y-position (chord/beam/nearest?) into account
225 scmify forced-fraction
228 Beam::set_stem_shorten ()
230 if (!visible_stem_count ())
233 Real forced_fraction = forced_stem_count () / visible_stem_count ();
234 if (forced_fraction < 0.5)
237 int multiplicity = get_multiplicity ();
240 SCM shorten = scm_eval (ly_symbol2scm ("beamed-stem-shorten"));
242 if (shorten == SCM_EOL)
245 int sz = scm_ilength (shorten);
247 Staff_symbol_referencer_interface st (this);
248 Real staff_space = st.staff_space ();
249 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
250 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
252 /* cute, but who invented this -- how to customise ? */
253 if (forced_fraction < 1)
256 for (int i=0; i < stem_count (); i++)
259 if (s->invisible_b ())
261 if (gh_number_p (s->get_elt_property ("shorten")))
262 s->set_elt_property ("shorten", gh_double2scm (shorten_f));
267 Set elt properties height and y-position if not set.
268 Adjust stem lengths to reach beam.
271 Beam::after_line_breaking ()
273 /* first, calculate y, dy */
275 calc_default_position_and_height (&y, &dy);
276 if (visible_stem_count ())
278 if (suspect_slope_b (y, dy))
281 Real damped_dy = calc_slope_damping_f (dy);
282 Real quantised_dy = quantise_dy_f (damped_dy);
284 y += (dy - quantised_dy) / 2;
288 until here, we used only stem_info, which acts as if dir=up
290 y *= directional_element (this).get ();
291 dy *= directional_element (this).get ();
293 Staff_symbol_referencer_interface st (this);
294 Real half_space = st.staff_space () / 2;
296 /* check for user-override of dy */
297 SCM s = remove_elt_property ("height-hs");
300 dy = gh_scm2double (s) * half_space;
302 set_elt_property ("height", gh_double2scm (dy));
304 /* check for user-override of y */
305 s = remove_elt_property ("y-position-hs");
308 y = gh_scm2double (s) * half_space;
312 /* we can modify y, so we should quantise y */
313 Real y_shift = check_stem_length_f (y, dy);
315 y = quantise_y_f (y, dy, 0);
316 set_stem_length (y, dy);
317 y_shift = check_stem_length_f (y, dy);
319 if (y_shift > half_space / 4)
324 for significantly lengthened or shortened stems,
325 request quanting the other way.
328 if (abs (y_shift) > half_space / 2)
329 quant_dir = sign (y_shift) * directional_element (this).get ();
330 y = quantise_y_f (y, dy, quant_dir);
333 // UGH. Y is not in staff position unit?
334 // Ik dacht datwe daar juist van weg wilden?
335 set_stem_length (y, dy);
336 set_elt_property ("y-position", gh_double2scm (y));
340 See Documentation/tex/fonts.doc
343 Beam::calc_default_position_and_height (Real* y, Real* dy) const
347 if (visible_stem_count () <= 1)
350 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
351 if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
358 Array<Offset> ideals;
359 Real x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
360 for (int i=0; i < stem_count (); i++)
363 if (s->invisible_b ())
365 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
366 s->calc_stem_info ().idealy_f_));
369 minimise_least_squares (&dydx, y, ideals); // duh, takes references
371 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
376 Beam::suspect_slope_b (Real y, Real dy) const
378 /* first, calculate y, dy */
380 steep slope running against lengthened stem is suspect
382 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
383 Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
384 Real lengthened = paper_l ()->get_var ("beam_lengthened");
385 Real steep = paper_l ()->get_var ("beam_steep_slope");
387 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - first_visible_stem ()->relative_coordinate (0, X_AXIS);
388 Real dydx = dy && dx ? dy/dx : 0;
390 if (((y - first_ideal > lengthened) && (dydx > steep))
391 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
399 This neat trick is by Werner Lemberg,
400 damped = tanh (slope)
401 corresponds with some tables in [Wanske]
404 Beam::calc_slope_damping_f (Real dy) const
406 SCM damp = get_elt_property ("damping"); // remove?
407 int damping = 1; // ugh.
408 if (gh_number_p (damp))
409 damping = gh_scm2int (damp);
413 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS)
414 - first_visible_stem ()->relative_coordinate (0, X_AXIS);
415 Real dydx = dy && dx ? dy/dx : 0;
416 dydx = 0.6 * tanh (dydx) / damping;
423 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
425 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
426 thick *= paper_l ()->get_var ("staffspace");
428 int beam_multiplicity = get_multiplicity ();
429 int stem_multiplicity = (s->flag_i () - 2) >? 0;
431 Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
432 Real x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
433 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
434 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
437 Direction dir = directional_element(this).get ();
438 Direction sdir = directional_element (s).get ();
444 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
446 Staff_symbol_referencer_interface me (s);
447 Staff_symbol_referencer_interface last (last_visible_stem ());
449 // huh, why not for first visible?
450 if (//(s != first_visible_stem ()) &&
451 me.staff_symbol_l () != last.staff_symbol_l ())
452 stem_y += directional_element (this).get ()
453 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
459 Beam::check_stem_length_f (Real y, Real dy) const
463 Direction dir = directional_element (this).get ();
465 for (int i=0; i < stem_count (); i++)
468 if (s->invisible_b ())
471 Real stem_y = calc_stem_y_f (s, y, dy);
474 Stem_info info = s->calc_stem_info ();
476 // if (0 > info.maxy_f_ - stem_y)
477 shorten = shorten <? info.maxy_f_ - stem_y;
478 // if (0 < info.miny_f_ - stem_y)
479 lengthen = lengthen >? info.miny_f_ - stem_y;
482 if (lengthen && shorten)
483 warning (_ ("weird beam vertical offset"));
485 /* when all stems are too short, normal stems win */
486 return dir * ((shorten) ? shorten : lengthen);
490 Hmm. At this time, beam position and slope are determined. Maybe,
491 stem directions and length should set to relative to the chord's
492 position of the beam. */
494 Beam::set_stem_length (Real y, Real dy)
496 Staff_symbol_referencer_interface st (this);
497 Real half_space = st.staff_space ()/2;
498 for (int i=0; i < stem_count (); i++)
501 if (s->invisible_b ())
504 Real stem_y = calc_stem_y_f (s, y, dy);
506 /* caution: stem measures in staff-positions */
507 s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
512 [Ross] (simplification of)
513 Set dy complying with:
515 - thick / 2 + staffline_f / 2
516 - thick + staffline_f
520 Beam::quantise_dy_f (Real dy) const
523 for (SCM s = scm_eval (ly_symbol2scm ("beam-height-quants")); s !=SCM_EOL; s = gh_cdr (s))
524 a.push (gh_scm2double (gh_car (s)));
529 Staff_symbol_referencer_interface st (this);
530 Real staff_space = st.staff_space ();
532 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
533 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
537 return q * sign (dy);
541 Prevent interference from stafflines and beams.
542 See Documentation/tex/fonts.doc
544 We only need to quantise the (left) y-position of the beam,
545 since dy is quantised too.
546 if extend_b then stems must *not* get shorter
549 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
551 int multiplicity = get_multiplicity ();
552 Staff_symbol_referencer_interface st (this);
553 Real staff_space = st.staff_space ();
554 SCM quants = scm_eval (gh_list (ly_symbol2scm ("beam-vertical-position-quants"),
555 gh_int2scm (multiplicity),
556 gh_double2scm (dy/staff_space),
561 for (; quants != SCM_EOL; quants = gh_cdr (quants))
562 a.push (gh_scm2double (gh_car (quants)));
567 Real up_y = directional_element (this).get () * y;
568 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
570 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
571 ? iv[SMALLER] : iv[BIGGER];
573 q = iv[(Direction)quant_dir];
575 return q * directional_element (this).get ();
579 Beam::set_beaming (Beaming_info_list *beaming)
582 for (int i=0; i < stem_count (); i++)
586 if (stem (i)->beam_count (d) == 0)
587 stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
589 while (flip (&d) != LEFT);
596 beams to go with one stem.
602 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
604 if ((next && !(next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
605 (prev && !(prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
606 programming_error ("Beams are not left-to-right");
608 Real staffline_f = paper_l ()->get_var ("stafflinethickness");
609 int multiplicity = get_multiplicity ();
612 Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
613 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
614 thick *= paper_l ()->get_var ("staffspace");
616 Real bdy = interbeam_f;
617 Real stemdx = staffline_f;
619 Real dx = visible_stem_count () ?
620 last_visible_stem ()->relative_coordinate (0, X_AXIS) - first_visible_stem ()->relative_coordinate (0, X_AXIS)
622 Real dy = gh_scm2double (get_elt_property ("height"));
623 Real dydx = dy && dx ? dy/dx : 0;
630 if (!here->first_head ())
632 else if (here->type_i ()== 1)
633 nw_f = paper_l ()->get_var ("wholewidth");
634 else if (here->type_i () == 2)
635 nw_f = paper_l ()->get_var ("notewidth") * 0.8;
637 nw_f = paper_l ()->get_var ("quartwidth");
640 Direction dir = directional_element (this).get ();
642 /* half beams extending to the left. */
645 int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
646 int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
648 Half beam should be one note-width,
649 but let's make sure two half-beams never touch
651 Real w = here->relative_coordinate (0, X_AXIS) - prev->relative_coordinate (0, X_AXIS);
654 if (lhalfs) // generates warnings if not
655 a = lookup_l ()->beam (dydx, w, thick);
656 a.translate (Offset (-w, -w * dydx));
657 for (int j = 0; j < lhalfs; j++)
660 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
661 leftbeams.add_molecule (b);
667 int rhalfs = here->beam_count (RIGHT) - next->beam_count (LEFT);
668 int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
670 Real w = next->relative_coordinate (0, X_AXIS) - here->relative_coordinate (0, X_AXIS);
671 Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
672 a.translate_axis( - stemdx/2, X_AXIS);
676 SCM gap = get_elt_property ("beam-gap");
677 if (gh_number_p (gap))
679 int gap_i = gh_scm2int ( (gap));
680 int nogap = rwholebeams - gap_i;
682 for (; j < nogap; j++)
685 b.translate_axis (-dir * bdy * j, Y_AXIS);
686 rightbeams.add_molecule (b);
688 // TODO: notehead widths differ for different types
691 a = lookup_l ()->beam (dydx, w + stemdx, thick);
694 for (; j < rwholebeams; j++)
697 b.translate (Offset (here->invisible_b () ? 0 : gap_f, -dir * bdy * j));
698 rightbeams.add_molecule (b);
703 a = lookup_l ()->beam (dydx, w, thick);
705 for (; j < rwholebeams + rhalfs; j++)
708 b.translate_axis (- dir * bdy * j, Y_AXIS);
709 rightbeams.add_molecule (b);
713 leftbeams.add_molecule (rightbeams);
716 Does beam quanting think of the asymetry of beams?
717 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
722 MAKE_SCHEME_SCORE_ELEMENT_CALLBACKS (Beam);
725 Beam::do_brew_molecule () const
731 if (visible_stem_count ())
733 x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
734 dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
738 x0 = stem (0)->relative_coordinate (0, X_AXIS);
739 dx = stem_top ()->relative_coordinate (0, X_AXIS) - x0;
743 Real dy = gh_scm2double (get_elt_property ("height"));
744 Real dydx = dy && dx ? dy/dx : 0;
745 Real y = gh_scm2double (get_elt_property ("y-position"));
746 for (int j=0; j <stem_count (); j++)
749 Stem * prev = (j > 0)? stem (j-1) : 0;
750 Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
752 Molecule sb = stem_beams (i, next, prev);
753 Real x = i->relative_coordinate (0, X_AXIS)-x0;
754 sb.translate (Offset (x, x * dydx + y));
755 mol.add_molecule (sb);
757 mol.translate_axis (x0
758 - get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
764 Beam::forced_stem_count () const
767 for (int i=0; i < stem_count (); i++)
771 if (s->invisible_b ())
774 if (((int)s->chord_start_f ())
775 && (s->get_direction () != s->get_default_dir ()))
782 TODO: Fix this class. This is wildly inefficient.
783 And it sux. Yet another array/list 'interface'.
786 Beam::stem (int i) const
788 return Pointer_group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
792 Beam::stem_count () const
794 Pointer_group_interface gi (this, "stems");
799 Beam::stem_top () const
801 SCM s = get_elt_pointer ("stems");
803 return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
808 Beam::visible_stem_count () const
811 for (int i = 0; i < stem_count (); i++)
813 if (!stem (i)->invisible_b ())
820 Beam::first_visible_stem () const
822 for (int i = 0; i < stem_count (); i++)
825 if (!s->invisible_b ())
832 Beam::last_visible_stem () const
834 for (int i = stem_count (); i > 0; i--)
836 Stem* s = stem (i - 1);
837 if (!s->invisible_b ())