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
19 #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 Group_interface g (this, "stems");
43 Beam::add_stem (Stem*s)
45 Group_interface gi (this, "stems");
48 s->add_dependency (this);
50 assert (!s->beam_l ());
51 s->set_elt_property ("beam", self_scm_);
53 if (!get_bound (LEFT))
60 Beam::get_multiplicity () const
63 for (SCM s = get_elt_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
65 Score_element * sc = unsmob_element (gh_car (s));
67 if (Stem * st = dynamic_cast<Stem*> (sc))
68 m = m >? st->beam_count (LEFT) >? st->beam_count (RIGHT);
74 After pre-processing all directions should be set.
75 Several post-processing routines (stem, slur, script) need stem/beam
77 Currenly, this means that beam has set all stem's directions.
78 [Alternatively, stems could set its own directions, according to
79 their beam, during 'final-pre-processing'.]
82 Beam::before_line_breaking ()
85 if (visible_stem_count () < 2)
87 warning (_ ("beam has less than two stems"));
88 // set_elt_property ("transparent", SCM_BOOL_T);
91 if (!directional_element (this).get ())
92 directional_element (this).set (get_default_dir ());
95 set_stem_directions ();
103 Beam::get_default_dir () const
105 Drul_array<int> total;
106 total[UP] = total[DOWN] = 0;
107 Drul_array<int> count;
108 count[UP] = count[DOWN] = 0;
111 for (int i=0; i <stem_count (); i++)
112 do { // HUH -- waar slaat dit op?
114 Direction sd = directional_element (s).get ();
115 int current = sd ? (1 + d * sd)/2
116 : s->get_center_distance ((Direction)-d);
124 } while (flip(&d) != DOWN);
127 SCM s = scm_eval (gh_list (ly_symbol2scm ("beam-dir-algorithm"),
128 ly_quote_scm (gh_cons (gh_int2scm (count[UP]),
129 gh_int2scm (count[DOWN]))),
130 ly_quote_scm (gh_cons (gh_int2scm (total[UP]),
131 gh_int2scm (total[DOWN]))),
133 if (gh_number_p (s) && gh_scm2int (s))
137 If dir is not determined: get from paper
139 return (Direction)(int)
140 paper_l ()->get_var ("stem_default_neutral_direction");
145 Set all stems with non-forced direction to beam direction.
146 Urg: non-forced should become `without/with unforced' direction,
147 once stem gets cleaned-up.
150 Beam::set_stem_directions ()
152 Direction d = directional_element (this).get ();
153 for (int i=0; i <stem_count (); i++)
156 SCM force = s->remove_elt_property ("dir-forced");
157 if (!gh_boolean_p (force) || !gh_scm2bool (force))
158 directional_element (s).set (d);
165 if (!auto_knee ("auto-interstaff-knee-gap", true))
166 auto_knee ("auto-knee-gap", false);
170 Simplistic auto-knees; only consider vertical gap between two
173 `Forced' stem directions are ignored. If you don't want auto-knees,
174 don't set, or unset autoKneeGap/autoInterstaffKneeGap.
177 Beam::auto_knee (String gap_str, bool interstaff_b)
181 SCM gap = get_elt_property (gap_str);
182 Direction d = directional_element (this).get ();
184 if (gh_number_p (gap))
186 int auto_gap_i = gh_scm2int (gap);
187 for (int i=1; i < stem_count (); i++)
189 bool is_b = (bool)(calc_interstaff_dist (stem (i), this)
190 - calc_interstaff_dist (stem (i-1), this));
191 int l_y = (int)(stem (i-1)->head_positions()[d])
192 + (int)calc_interstaff_dist (stem (i-1), this);
193 int r_y = (int)(stem (i)->head_positions()[d])
194 + (int)calc_interstaff_dist (stem (i), this);
195 int gap_i = r_y - l_y;
197 if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
199 knee_y = (r_y + l_y) / 2;
207 for (int i=0; i < stem_count (); i++)
209 int y = (int)(stem (i)->head_positions()[d])
210 + (int)calc_interstaff_dist (stem (i), this);
211 directional_element (stem (i)).set (y < knee_y ? UP : DOWN);
212 stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
219 Set stem's shorten property if unset.
221 take some y-position (chord/beam/nearest?) into account
222 scmify forced-fraction
225 Beam::set_stem_shorten ()
227 if (!visible_stem_count ())
230 Real forced_fraction = forced_stem_count () / visible_stem_count ();
231 if (forced_fraction < 0.5)
234 int multiplicity = get_multiplicity ();
237 SCM shorten = scm_eval (ly_symbol2scm ("beamed-stem-shorten"));
239 if (shorten == SCM_EOL)
242 int sz = scm_ilength (shorten);
244 Staff_symbol_referencer_interface st (this);
245 Real staff_space = st.staff_space ();
246 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
247 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
249 /* cute, but who invented this -- how to customise ? */
250 if (forced_fraction < 1)
253 for (int i=0; i < stem_count (); i++)
256 if (s->invisible_b ())
258 if (gh_number_p (s->get_elt_property ("shorten")))
259 s->set_elt_property ("shorten", gh_double2scm (shorten_f));
264 Set elt properties height and y-position if not set.
265 Adjust stem lengths to reach beam.
268 Beam::after_line_breaking ()
270 /* first, calculate y, dy */
272 calc_position_and_height (&y, &dy);
273 if (visible_stem_count ())
275 if (suspect_slope_b (y, dy))
278 Real damped_dy = calc_slope_damping_f (dy);
279 Real quantised_dy = quantise_dy_f (damped_dy);
281 y += (dy - quantised_dy) / 2;
285 until here, we used only stem_info, which acts as if dir=up
287 y *= directional_element (this).get ();
288 dy *= directional_element (this).get ();
290 Staff_symbol_referencer_interface st (this);
291 Real half_space = st.staff_space () / 2;
293 /* check for user-override of dy */
294 SCM s = remove_elt_property ("height-hs");
297 dy = gh_scm2double (s) * half_space;
299 set_elt_property ("height", gh_double2scm (dy));
301 /* check for user-override of y */
302 s = remove_elt_property ("y-position-hs");
305 y = gh_scm2double (s) * half_space;
309 /* we can modify y, so we should quantise y */
310 Real y_shift = check_stem_length_f (y, dy);
312 y = quantise_y_f (y, dy, 0);
313 set_stem_length (y, dy);
314 y_shift = check_stem_length_f (y, dy);
316 if (y_shift > half_space / 4)
321 for significantly lengthened or shortened stems,
322 request quanting the other way.
325 if (abs (y_shift) > half_space / 2)
326 quant_dir = sign (y_shift) * directional_element (this).get ();
327 y = quantise_y_f (y, dy, quant_dir);
330 // UGH. Y is not in staff position unit?
331 // Ik dacht datwe daar juist van weg wilden?
332 set_stem_length (y, dy);
333 set_elt_property ("y-position", gh_double2scm (y));
337 See Documentation/tex/fonts.doc
340 Beam::calc_position_and_height (Real* y, Real* dy) const
343 if (visible_stem_count () <= 1)
346 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
347 if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
354 Array<Offset> ideals;
355 Real x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
356 for (int i=0; i < stem_count (); i++)
359 if (s->invisible_b ())
361 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
362 s->calc_stem_info ().idealy_f_));
365 minimise_least_squares (&dydx, y, ideals); // duh, takes references
367 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
372 Beam::suspect_slope_b (Real y, Real dy) const
374 /* first, calculate y, dy */
376 steep slope running against lengthened stem is suspect
378 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
379 Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
380 Real lengthened = paper_l ()->get_var ("beam_lengthened");
381 Real steep = paper_l ()->get_var ("beam_steep_slope");
383 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - first_visible_stem ()->relative_coordinate (0, X_AXIS);
384 Real dydx = dy && dx ? dy/dx : 0;
386 if (((y - first_ideal > lengthened) && (dydx > steep))
387 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
395 This neat trick is by Werner Lemberg,
396 damped = tanh (slope)
397 corresponds with some tables in [Wanske]
400 Beam::calc_slope_damping_f (Real dy) const
402 SCM damp = get_elt_property ("damping"); // remove?
403 int damping = 1; // ugh.
404 if (gh_number_p (damp))
405 damping = gh_scm2int (damp);
409 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS)
410 - first_visible_stem ()->relative_coordinate (0, X_AXIS);
411 Real dydx = dy && dx ? dy/dx : 0;
412 dydx = 0.6 * tanh (dydx) / damping;
419 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
421 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
422 int beam_multiplicity = get_multiplicity ();
423 int stem_multiplicity = (s->flag_i () - 2) >? 0;
425 Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
426 Real x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
427 Real dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
428 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
431 Direction dir = directional_element(this).get ();
432 Direction sdir = directional_element (s).get ();
438 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
440 Staff_symbol_referencer_interface me (s);
441 Staff_symbol_referencer_interface last (last_visible_stem ());
443 // huh, why not for first visible?
444 if (//(s != first_visible_stem ()) &&
445 me.staff_symbol_l () != last.staff_symbol_l ())
446 stem_y += directional_element (this).get ()
447 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
453 Beam::check_stem_length_f (Real y, Real dy) const
457 Direction dir = directional_element (this).get ();
459 for (int i=0; i < stem_count (); i++)
462 if (s->invisible_b ())
465 Real stem_y = calc_stem_y_f (s, y, dy);
468 Stem_info info = s->calc_stem_info ();
470 // if (0 > info.maxy_f_ - stem_y)
471 shorten = shorten <? info.maxy_f_ - stem_y;
472 // if (0 < info.miny_f_ - stem_y)
473 lengthen = lengthen >? info.miny_f_ - stem_y;
476 if (lengthen && shorten)
477 warning (_ ("weird beam vertical offset"));
479 /* when all stems are too short, normal stems win */
480 return dir * ((shorten) ? shorten : lengthen);
484 Hmm. At this time, beam position and slope are determined. Maybe,
485 stem directions and length should set to relative to the chord's
486 position of the beam. */
488 Beam::set_stem_length (Real y, Real dy)
490 Staff_symbol_referencer_interface st (this);
491 Real half_space = st.staff_space ()/2;
492 for (int i=0; i < stem_count (); i++)
495 if (s->invisible_b ())
498 Real stem_y = calc_stem_y_f (s, y, dy);
500 /* caution: stem measures in staff-positions */
501 s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
506 [Ross] (simplification of)
507 Set dy complying with:
509 - thick / 2 + staffline_f / 2
510 - thick + staffline_f
514 Beam::quantise_dy_f (Real dy) const
517 for (SCM s = scm_eval (ly_symbol2scm ("beam-height-quants")); s !=SCM_EOL; s = gh_cdr (s))
518 a.push (gh_scm2double (gh_car (s)));
523 Staff_symbol_referencer_interface st (this);
524 Real staff_space = st.staff_space ();
526 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
527 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
531 return q * sign (dy);
535 Prevent interference from stafflines and beams.
536 See Documentation/tex/fonts.doc
538 We only need to quantise the (left) y-position of the beam,
539 since dy is quantised too.
540 if extend_b then stems must *not* get shorter
543 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
545 int multiplicity = get_multiplicity ();
546 Staff_symbol_referencer_interface st (this);
547 Real staff_space = st.staff_space ();
548 SCM quants = scm_eval (gh_list (ly_symbol2scm ("beam-vertical-position-quants"),
549 gh_int2scm (multiplicity),
550 gh_double2scm (dy/staff_space),
555 for (; quants != SCM_EOL; quants = gh_cdr (quants))
556 a.push (gh_scm2double (gh_car (quants)));
561 Real up_y = directional_element (this).get () * y;
562 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
564 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
565 ? iv[SMALLER] : iv[BIGGER];
567 q = iv[(Direction)quant_dir];
569 return q * directional_element (this).get ();
573 Beam::set_beaming (Beaming_info_list *beaming)
576 for (int i=0; i < stem_count (); i++)
580 if (stem (i)->beam_count (d) == 0)
581 stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
583 while (flip (&d) != LEFT);
590 beams to go with one stem.
596 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
598 if ((next && !(next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
599 (prev && !(prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
600 programming_error ("Beams are not left-to-right");
602 Real staffline_f = paper_l ()->get_var ("stafflinethickness");
603 int multiplicity = get_multiplicity ();
606 Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
607 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
609 Real bdy = interbeam_f;
610 Real stemdx = staffline_f;
612 Real dx = visible_stem_count () ?
613 last_visible_stem ()->relative_coordinate (0, X_AXIS) - first_visible_stem ()->relative_coordinate (0, X_AXIS)
615 Real dy = gh_scm2double (get_elt_property ("height"));
616 Real dydx = dy && dx ? dy/dx : 0;
623 if (!here->first_head ())
625 else if (here->type_i ()== 1)
626 nw_f = paper_l ()->get_var ("wholewidth");
627 else if (here->type_i () == 2)
628 nw_f = paper_l ()->get_var ("notewidth") * 0.8;
630 nw_f = paper_l ()->get_var ("quartwidth");
633 Direction dir = directional_element (this).get ();
635 /* half beams extending to the left. */
638 int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
639 int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
641 Half beam should be one note-width,
642 but let's make sure two half-beams never touch
644 Real w = here->relative_coordinate (0, X_AXIS) - prev->relative_coordinate (0, X_AXIS);
647 if (lhalfs) // generates warnings if not
648 a = lookup_l ()->beam (dydx, w, thick);
649 a.translate (Offset (-w, -w * dydx));
650 for (int j = 0; j < lhalfs; j++)
653 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
654 leftbeams.add_molecule (b);
660 int rhalfs = here->beam_count (RIGHT) - next->beam_count (LEFT);
661 int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
663 Real w = next->relative_coordinate (0, X_AXIS) - here->relative_coordinate (0, X_AXIS);
664 Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
665 a.translate_axis( - stemdx/2, X_AXIS);
669 SCM gap = get_elt_property ("beam-gap");
670 if (gh_number_p (gap))
672 int gap_i = gh_scm2int ( (gap));
673 int nogap = rwholebeams - gap_i;
675 for (; j < nogap; j++)
678 b.translate_axis (-dir * bdy * j, Y_AXIS);
679 rightbeams.add_molecule (b);
681 // TODO: notehead widths differ for different types
684 a = lookup_l ()->beam (dydx, w + stemdx, thick);
687 for (; j < rwholebeams; j++)
690 b.translate (Offset (here->invisible_b () ? 0 : gap_f, -dir * bdy * j));
691 rightbeams.add_molecule (b);
696 a = lookup_l ()->beam (dydx, w, thick);
698 for (; j < rwholebeams + rhalfs; j++)
701 b.translate_axis (- dir * bdy * j, Y_AXIS);
702 rightbeams.add_molecule (b);
706 leftbeams.add_molecule (rightbeams);
709 Does beam quanting think of the asymetry of beams?
710 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
717 Beam::do_brew_molecule () const
723 if (visible_stem_count ())
725 x0 = first_visible_stem ()->relative_coordinate (0, X_AXIS);
726 dx = last_visible_stem ()->relative_coordinate (0, X_AXIS) - x0;
730 x0 = stem (0)->relative_coordinate (0, X_AXIS);
731 dx = stem_top ()->relative_coordinate (0, X_AXIS) - x0;
735 Real dy = gh_scm2double (get_elt_property ("height"));
736 Real dydx = dy && dx ? dy/dx : 0;
737 Real y = gh_scm2double (get_elt_property ("y-position"));
738 for (int j=0; j <stem_count (); j++)
741 Stem * prev = (j > 0)? stem (j-1) : 0;
742 Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
744 Molecule sb = stem_beams (i, next, prev);
745 Real x = i->relative_coordinate (0, X_AXIS)-x0;
746 sb.translate (Offset (x, x * dydx + y));
747 mol.add_molecule (sb);
749 mol.translate_axis (x0
750 - get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
756 Beam::forced_stem_count () const
759 for (int i=0; i < stem_count (); i++)
763 if (s->invisible_b ())
766 if (((int)s->chord_start_f ())
767 && (s->get_direction () != s->get_default_dir ()))
774 TODO: Fix this class. This is wildly inefficient.
775 And it sux. Yet another array/list 'interface'.
778 Beam::stem (int i) const
780 return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
784 Beam::stem_count () const
786 Group_interface gi (this, "stems");
791 Beam::stem_top () const
793 SCM s = get_elt_property ("stems");
795 return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
800 Beam::visible_stem_count () const
803 for (int i = 0; i < stem_count (); i++)
805 if (!stem (i)->invisible_b ())
812 Beam::first_visible_stem () const
814 for (int i = 0; i < stem_count (); i++)
817 if (!s->invisible_b ())
824 Beam::last_visible_stem () const
826 for (int i = stem_count (); i > 0; i--)
828 Stem* s = stem (i - 1);
829 if (!s->invisible_b ())