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"
37 Group_interface g (this, "stems");
40 set_elt_property ("height", gh_int2scm (0)); // ugh.
41 set_elt_property ("y-position" ,gh_int2scm (0));
45 Beam::add_stem (Stem*s)
47 Group_interface gi (this, "stems");
50 s->add_dependency (this);
52 assert (!s->beam_l ());
53 s->set_elt_property ("beam", self_scm_);
55 if (!get_bound (LEFT))
62 Beam::get_multiplicity () const
65 for (SCM s = get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
67 Score_element * sc = unsmob_element (gh_car (s));
69 if (Stem * st = dynamic_cast<Stem*> (sc))
70 m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
76 After pre-processing all directions should be set.
77 Several post-processing routines (stem, slur, script) need stem/beam
79 Currenly, this means that beam has set all stem's directions.
80 [Alternatively, stems could set its own directions, according to
81 their beam, during 'final-pre-processing'.]
84 Beam::before_line_breaking ()
87 if (visible_stem_count () < 2)
89 warning (_ ("beam has less than two stems"));
90 // set_elt_property ("transparent", SCM_BOOL_T);
93 if (!directional_element (this).get ())
94 directional_element (this).set (get_default_dir ());
97 set_stem_directions ();
105 Beam::get_default_dir () const
107 Drul_array<int> total;
108 total[UP] = total[DOWN] = 0;
109 Drul_array<int> count;
110 count[UP] = count[DOWN] = 0;
113 for (int i=0; i <stem_count (); i++)
114 do { // HUH -- waar slaat dit op?
116 Direction sd = directional_element (s).get ();
117 int current = sd ? (1 + d * sd)/2
118 : s->get_center_distance ((Direction)-d);
126 } while (flip(&d) != DOWN);
129 SCM s = scm_eval (gh_list (ly_symbol2scm ("beam-dir-algorithm"),
130 ly_quote_scm (gh_cons (gh_int2scm (count[UP]),
131 gh_int2scm (count[DOWN]))),
132 ly_quote_scm (gh_cons (gh_int2scm (total[UP]),
133 gh_int2scm (total[DOWN]))),
135 if (gh_number_p (s) && gh_scm2int (s))
139 If dir is not determined: get from paper
141 return (Direction)(int)
142 paper_l ()->get_var ("stem_default_neutral_direction");
147 Set all stems with non-forced direction to beam direction.
148 Urg: non-forced should become `without/with unforced' direction,
149 once stem gets cleaned-up.
152 Beam::set_stem_directions ()
154 Direction d = directional_element (this).get ();
155 for (int i=0; i <stem_count (); i++)
158 SCM force = s->remove_elt_property ("dir-forced");
159 if (!gh_boolean_p (force) || !gh_scm2bool (force))
160 directional_element (s).set (d);
167 if (!auto_knee ("auto-interstaff-knee-gap", true))
168 auto_knee ("auto-knee-gap", false);
172 Simplistic auto-knees; only consider vertical gap between two
175 `Forced' stem directions are ignored. If you don't want auto-knees,
176 don't set, or unset autoKneeGap/autoInterstaffKneeGap.
179 Beam::auto_knee (String gap_str, bool interstaff_b)
183 SCM gap = get_elt_property (gap_str);
184 Direction d = directional_element (this).get ();
186 if (gh_number_p (gap))
188 int auto_gap_i = gh_scm2int (gap);
189 for (int i=1; i < stem_count (); i++)
191 bool is_b = (bool)(calc_interstaff_dist (stem (i), this)
192 - calc_interstaff_dist (stem (i-1), this));
193 int l_y = (int)(stem (i-1)->head_positions()[d])
194 + (int)calc_interstaff_dist (stem (i-1), this);
195 int r_y = (int)(stem (i)->head_positions()[d])
196 + (int)calc_interstaff_dist (stem (i), this);
197 int gap_i = r_y - l_y;
199 if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
201 knee_y = (r_y + l_y) / 2;
209 for (int i=0; i < stem_count (); i++)
211 int y = (int)(stem (i)->head_positions()[d])
212 + (int)calc_interstaff_dist (stem (i), this);
213 directional_element (stem (i)).set (y < knee_y ? UP : DOWN);
214 stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
221 Set stem's shorten property if unset.
223 take some y-position (chord/beam/nearest?) into account
224 scmify forced-fraction
227 Beam::set_stem_shorten ()
229 if (!visible_stem_count ())
232 Real forced_fraction = forced_stem_count () / visible_stem_count ();
233 if (forced_fraction < 0.5)
236 int multiplicity = get_multiplicity ();
239 SCM shorten = scm_eval (ly_symbol2scm ("beamed-stem-shorten"));
241 if (shorten == SCM_EOL)
244 int sz = scm_ilength (shorten);
246 Staff_symbol_referencer_interface st (this);
247 Real staff_space = st.staff_space ();
248 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
249 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
251 /* cute, but who invented this -- how to customise ? */
252 if (forced_fraction < 1)
255 for (int i=0; i < stem_count (); i++)
258 if (s->invisible_b ())
260 if (gh_number_p (s->get_elt_property ("shorten")))
261 s->set_elt_property ("shorten", gh_double2scm (shorten_f));
266 Set elt properties height and y-position if not set.
267 Adjust stem lengths to reach beam.
270 Beam::after_line_breaking ()
272 /* first, calculate y, dy */
274 calc_default_position_and_height (&y, &dy);
275 if (visible_stem_count ())
277 if (suspect_slope_b (y, dy))
280 Real damped_dy = calc_slope_damping_f (dy);
281 Real quantised_dy = quantise_dy_f (damped_dy);
283 y += (dy - quantised_dy) / 2;
287 until here, we used only stem_info, which acts as if dir=up
289 y *= directional_element (this).get ();
290 dy *= directional_element (this).get ();
292 Staff_symbol_referencer_interface st (this);
293 Real half_space = st.staff_space () / 2;
295 /* check for user-override of dy */
296 SCM s = remove_elt_property ("height-hs");
299 dy = gh_scm2double (s) * half_space;
301 set_elt_property ("height", gh_double2scm (dy));
303 /* check for user-override of y */
304 s = remove_elt_property ("y-position-hs");
307 y = gh_scm2double (s) * half_space;
311 /* we can modify y, so we should quantise y */
312 Real y_shift = check_stem_length_f (y, dy);
314 y = quantise_y_f (y, dy, 0);
315 set_stem_length (y, dy);
316 y_shift = check_stem_length_f (y, dy);
318 if (y_shift > half_space / 4)
323 for significantly lengthened or shortened stems,
324 request quanting the other way.
327 if (abs (y_shift) > half_space / 2)
328 quant_dir = sign (y_shift) * directional_element (this).get ();
329 y = quantise_y_f (y, dy, quant_dir);
332 // UGH. Y is not in staff position unit?
333 // Ik dacht datwe daar juist van weg wilden?
334 set_stem_length (y, dy);
335 set_elt_property ("y-position", gh_double2scm (y));
339 See Documentation/tex/fonts.doc
342 Beam::calc_default_position_and_height (Real* y, Real* dy) const
346 if (visible_stem_count () <= 1)
349 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
350 if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
357 Array<Offset> ideals;
358 Real x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
359 for (int i=0; i < stem_count (); i++)
362 if (s->invisible_b ())
364 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
365 s->calc_stem_info ().idealy_f_));
368 minimise_least_squares (&dydx, y, ideals); // duh, takes references
370 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
375 Beam::suspect_slope_b (Real y, Real dy) const
377 /* first, calculate y, dy */
379 steep slope running against lengthened stem is suspect
381 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
382 Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
383 Real lengthened = paper_l ()->get_var ("beam_lengthened");
384 Real steep = paper_l ()->get_var ("beam_steep_slope");
386 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - first_visible_stem ()->relative_coordinate (0, X_AXIS);
387 Real dydx = dy && dx ? dy/dx : 0;
389 if (((y - first_ideal > lengthened) && (dydx > steep))
390 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
398 This neat trick is by Werner Lemberg,
399 damped = tanh (slope)
400 corresponds with some tables in [Wanske]
403 Beam::calc_slope_damping_f (Real dy) const
405 SCM damp = get_elt_property ("damping"); // remove?
406 int damping = 1; // ugh.
407 if (gh_number_p (damp))
408 damping = gh_scm2int (damp);
412 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS)
413 - first_visible_stem ()->relative_coordinate (0, X_AXIS);
414 Real dydx = dy && dx ? dy/dx : 0;
415 dydx = 0.6 * tanh (dydx) / damping;
422 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
424 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
425 int beam_multiplicity = get_multiplicity ();
426 int stem_multiplicity = (s->flag_i () - 2) >? 0;
428 Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
429 Real x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
430 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
431 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
434 Direction dir = directional_element(this).get ();
435 Direction sdir = directional_element (s).get ();
441 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
443 Staff_symbol_referencer_interface me (s);
444 Staff_symbol_referencer_interface last (last_visible_stem ());
446 // huh, why not for first visible?
447 if (//(s != first_visible_stem ()) &&
448 me.staff_symbol_l () != last.staff_symbol_l ())
449 stem_y += directional_element (this).get ()
450 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
456 Beam::check_stem_length_f (Real y, Real dy) const
460 Direction dir = directional_element (this).get ();
462 for (int i=0; i < stem_count (); i++)
465 if (s->invisible_b ())
468 Real stem_y = calc_stem_y_f (s, y, dy);
471 Stem_info info = s->calc_stem_info ();
473 // if (0 > info.maxy_f_ - stem_y)
474 shorten = shorten <? info.maxy_f_ - stem_y;
475 // if (0 < info.miny_f_ - stem_y)
476 lengthen = lengthen >? info.miny_f_ - stem_y;
479 if (lengthen && shorten)
480 warning (_ ("weird beam vertical offset"));
482 /* when all stems are too short, normal stems win */
483 return dir * ((shorten) ? shorten : lengthen);
487 Hmm. At this time, beam position and slope are determined. Maybe,
488 stem directions and length should set to relative to the chord's
489 position of the beam. */
491 Beam::set_stem_length (Real y, Real dy)
493 Staff_symbol_referencer_interface st (this);
494 Real half_space = st.staff_space ()/2;
495 for (int i=0; i < stem_count (); i++)
498 if (s->invisible_b ())
501 Real stem_y = calc_stem_y_f (s, y, dy);
503 /* caution: stem measures in staff-positions */
504 s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
509 [Ross] (simplification of)
510 Set dy complying with:
512 - thick / 2 + staffline_f / 2
513 - thick + staffline_f
517 Beam::quantise_dy_f (Real dy) const
520 for (SCM s = scm_eval (ly_symbol2scm ("beam-height-quants")); s !=SCM_EOL; s = gh_cdr (s))
521 a.push (gh_scm2double (gh_car (s)));
526 Staff_symbol_referencer_interface st (this);
527 Real staff_space = st.staff_space ();
529 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
530 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
534 return q * sign (dy);
538 Prevent interference from stafflines and beams.
539 See Documentation/tex/fonts.doc
541 We only need to quantise the (left) y-position of the beam,
542 since dy is quantised too.
543 if extend_b then stems must *not* get shorter
546 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
548 int multiplicity = get_multiplicity ();
549 Staff_symbol_referencer_interface st (this);
550 Real staff_space = st.staff_space ();
551 SCM quants = scm_eval (gh_list (ly_symbol2scm ("beam-vertical-position-quants"),
552 gh_int2scm (multiplicity),
553 gh_double2scm (dy/staff_space),
558 for (; quants != SCM_EOL; quants = gh_cdr (quants))
559 a.push (gh_scm2double (gh_car (quants)));
564 Real up_y = directional_element (this).get () * y;
565 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
567 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
568 ? iv[SMALLER] : iv[BIGGER];
570 q = iv[(Direction)quant_dir];
572 return q * directional_element (this).get ();
576 Beam::set_beaming (Beaming_info_list *beaming)
579 for (int i=0; i < stem_count (); i++)
583 if (stem (i)->beam_count (d) == 0)
584 stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
586 while (flip (&d) != LEFT);
593 beams to go with one stem.
599 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
601 if ((next && !(next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
602 (prev && !(prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
603 programming_error ("Beams are not left-to-right");
605 Real staffline_f = paper_l ()->get_var ("stafflinethickness");
606 int multiplicity = get_multiplicity ();
609 Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
610 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
612 Real bdy = interbeam_f;
613 Real stemdx = staffline_f;
615 Real dx = visible_stem_count () ?
616 last_visible_stem ()->relative_coordinate (0, X_AXIS) - first_visible_stem ()->relative_coordinate (0, X_AXIS)
618 Real dy = gh_scm2double (get_elt_property ("height"));
619 Real dydx = dy && dx ? dy/dx : 0;
626 if (!here->first_head ())
628 else if (here->type_i ()== 1)
629 nw_f = paper_l ()->get_var ("wholewidth");
630 else if (here->type_i () == 2)
631 nw_f = paper_l ()->get_var ("notewidth") * 0.8;
633 nw_f = paper_l ()->get_var ("quartwidth");
636 Direction dir = directional_element (this).get ();
638 /* half beams extending to the left. */
641 int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
642 int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
644 Half beam should be one note-width,
645 but let's make sure two half-beams never touch
647 Real w = here->relative_coordinate (0, X_AXIS) - prev->relative_coordinate (0, X_AXIS);
650 if (lhalfs) // generates warnings if not
651 a = lookup_l ()->beam (dydx, w, thick);
652 a.translate (Offset (-w, -w * dydx));
653 for (int j = 0; j < lhalfs; j++)
656 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
657 leftbeams.add_molecule (b);
663 int rhalfs = here->beam_count (RIGHT) - next->beam_count (LEFT);
664 int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
666 Real w = next->relative_coordinate (0, X_AXIS) - here->relative_coordinate (0, X_AXIS);
667 Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
668 a.translate_axis( - stemdx/2, X_AXIS);
672 SCM gap = get_elt_property ("beam-gap");
673 if (gh_number_p (gap))
675 int gap_i = gh_scm2int ( (gap));
676 int nogap = rwholebeams - gap_i;
678 for (; j < nogap; j++)
681 b.translate_axis (-dir * bdy * j, Y_AXIS);
682 rightbeams.add_molecule (b);
684 // TODO: notehead widths differ for different types
687 a = lookup_l ()->beam (dydx, w + stemdx, thick);
690 for (; j < rwholebeams; j++)
693 b.translate (Offset (here->invisible_b () ? 0 : gap_f, -dir * bdy * j));
694 rightbeams.add_molecule (b);
699 a = lookup_l ()->beam (dydx, w, thick);
701 for (; j < rwholebeams + rhalfs; j++)
704 b.translate_axis (- dir * bdy * j, Y_AXIS);
705 rightbeams.add_molecule (b);
709 leftbeams.add_molecule (rightbeams);
712 Does beam quanting think of the asymetry of beams?
713 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
720 Beam::do_brew_molecule () const
726 if (visible_stem_count ())
728 x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
729 dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
733 x0 = stem (0)->relative_coordinate (0, X_AXIS);
734 dx = stem_top ()->relative_coordinate (0, X_AXIS) - x0;
738 Real dy = gh_scm2double (get_elt_property ("height"));
739 Real dydx = dy && dx ? dy/dx : 0;
740 Real y = gh_scm2double (get_elt_property ("y-position"));
741 for (int j=0; j <stem_count (); j++)
744 Stem * prev = (j > 0)? stem (j-1) : 0;
745 Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
747 Molecule sb = stem_beams (i, next, prev);
748 Real x = i->relative_coordinate (0, X_AXIS)-x0;
749 sb.translate (Offset (x, x * dydx + y));
750 mol.add_molecule (sb);
752 mol.translate_axis (x0
753 - get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
759 Beam::forced_stem_count () const
762 for (int i=0; i < stem_count (); i++)
766 if (s->invisible_b ())
769 if (((int)s->chord_start_f ())
770 && (s->get_direction () != s->get_default_dir ()))
777 TODO: Fix this class. This is wildly inefficient.
778 And it sux. Yet another array/list 'interface'.
781 Beam::stem (int i) const
783 return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
787 Beam::stem_count () const
789 Group_interface gi (this, "stems");
794 Beam::stem_top () const
796 SCM s = get_elt_property ("stems");
798 return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
803 Beam::visible_stem_count () const
806 for (int i = 0; i < stem_count (); i++)
808 if (!stem (i)->invisible_b ())
815 Beam::first_visible_stem () const
817 for (int i = 0; i < stem_count (); i++)
820 if (!s->invisible_b ())
827 Beam::last_visible_stem () const
829 for (int i = stem_count (); i > 0; i--)
831 Stem* s = stem (i - 1);
832 if (!s->invisible_b ())