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
17 #include "directional-element-interface.hh"
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 (!directional_element (this).get ())
88 directional_element (this).set (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 Direction sd = directional_element (s).get ();
112 int current = sd ? (1 + d * sd)/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 = directional_element (this).get ();
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 directional_element (s).set (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 Direction d = directional_element (this).get ();
181 if (gh_number_p (gap))
183 int auto_gap_i = gh_scm2int (gap);
184 for (int i=1; i < stem_count (); i++)
186 bool is_b = (bool)(calc_interstaff_dist (stem (i), this)
187 - calc_interstaff_dist (stem (i-1), this));
188 int l_y = (int)(stem (i-1)->head_positions()[d])
189 + (int)calc_interstaff_dist (stem (i-1), this);
190 int r_y = (int)(stem (i)->head_positions()[d])
191 + (int)calc_interstaff_dist (stem (i), this);
192 int gap_i = r_y - l_y;
194 if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
196 knee_y = (r_y + l_y) / 2;
204 for (int i=0; i < stem_count (); i++)
206 int y = (int)(stem (i)->head_positions()[d])
207 + (int)calc_interstaff_dist (stem (i), this);
208 directional_element (stem (i)).set (y < knee_y ? UP : DOWN);
209 stem (i)->set_elt_property ("dir-forced", SCM_BOOL_T);
216 Set stem's shorten property if unset.
218 take some y-position (chord/beam/nearest?) into account
219 scmify forced-fraction
222 Beam::set_stem_shorten ()
224 if (!visible_stem_count ())
227 Real forced_fraction = forced_stem_count () / visible_stem_count ();
228 if (forced_fraction < 0.5)
231 int multiplicity = get_multiplicity ();
233 SCM shorten = ly_eval_str ("beamed-stem-shorten");
236 scm_to_array (shorten, &a);
240 Staff_symbol_referencer_interface st (this);
241 Real staff_space = st.staff_space ();
242 Real shorten_f = a[multiplicity <? (a.size () - 1)] * staff_space;
244 /* cute, but who invented this -- how to customise ? */
245 if (forced_fraction < 1)
248 for (int i=0; i < stem_count (); i++)
251 if (s->invisible_b ())
253 if (gh_number_p (s->get_elt_property ("shorten")))
254 s->set_elt_property ("shorten", gh_double2scm (shorten_f));
259 Set elt properties height and y-position if not set.
260 Adjust stem lengths to reach beam.
263 Beam::do_post_processing ()
265 /* first, calculate y, dy */
267 calc_position_and_height (&y, &dy);
268 if (suspect_slope_b (y, dy))
271 Real damped_dy = calc_slope_damping_f (dy);
272 Real quantised_dy = quantise_dy_f (damped_dy);
274 y += (dy - quantised_dy) / 2;
278 until here, we used only stem_info, which acts as if dir=up
280 y *= directional_element (this).get ();
281 dy *= directional_element (this).get ();
283 /* set or read dy as necessary */
284 SCM s = get_elt_property ("height");
286 dy = gh_scm2double (s);
288 set_elt_property ("height", gh_double2scm (dy));
290 /* set or read y as necessary */
291 s = get_elt_property ("y-position");
294 y = gh_scm2double (s);
295 set_stem_length (y, dy);
299 /* we can modify y, so we should quantise y */
300 Real y_shift = check_stem_length_f (y, dy);
302 y = quantise_y_f (y, dy, 0);
303 set_stem_length (y, dy);
304 y_shift = check_stem_length_f (y, dy);
306 Staff_symbol_referencer_interface st (this);
307 Real half_space = st.staff_space () / 2;
308 if (y_shift > half_space / 4)
313 for significantly lengthened or shortened stems,
314 request quanting the other way.
317 if (abs (y_shift) > half_space / 2)
318 quant_dir = sign (y_shift) * directional_element (this).get ();
319 y = quantise_y_f (y, dy, quant_dir);
320 set_stem_length (y, dy);
323 set_elt_property ("y-position", gh_double2scm (y));
328 See Documentation/tex/fonts.doc
331 Beam::calc_position_and_height (Real* y, Real* dy) const
334 if (visible_stem_count () <= 1)
337 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
338 if (first_ideal == last_visible_stem ()->calc_stem_info ().idealy_f_)
346 Real x0 = first_visible_stem ()->hpos_f ();
347 for (int i=0; i < stem_count (); i++)
350 if (s->invisible_b ())
352 ls.input.push (Offset (s->hpos_f () - x0,
353 s->calc_stem_info ().idealy_f_));
356 ls.minimise (dydx, *y); // duh, takes references
358 Real dx = last_visible_stem ()->hpos_f () - x0;
363 Beam::suspect_slope_b (Real y, Real dy) const
366 steep slope running against lengthened stem is suspect
368 Real first_ideal = first_visible_stem ()->calc_stem_info ().idealy_f_;
369 Real last_ideal = last_visible_stem ()->calc_stem_info ().idealy_f_;
370 Real lengthened = paper_l ()->get_var ("beam_lengthened");
371 Real steep = paper_l ()->get_var ("beam_steep_slope");
373 Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
374 Real dydx = dy && dx ? dy/dx : 0;
376 if (((y - first_ideal > lengthened) && (dydx > steep))
377 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
385 This neat trick is by Werner Lemberg,
386 damped = tanh (slope)
387 corresponds with some tables in [Wanske]
390 Beam::calc_slope_damping_f (Real dy) const
392 SCM damp = get_elt_property ("damping"); // remove?
393 int damping = 1; // ugh.
394 if (gh_number_p (damp))
395 damping = gh_scm2int (damp);
399 Real dx = last_visible_stem ()->hpos_f ()
400 - first_visible_stem ()->hpos_f ();
401 Real dydx = dy && dx ? dy/dx : 0;
402 dydx = 0.6 * tanh (dydx) / damping;
409 Beam::calc_stem_y_f (Stem* s, Real y, Real dy) const
411 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));
412 int beam_multiplicity = get_multiplicity ();
413 int stem_multiplicity = (s->flag_i () - 2) >? 0;
415 Real interbeam_f = paper_l ()->interbeam_f (beam_multiplicity);
416 Real x0 = first_visible_stem ()->hpos_f ();
417 Real dx = last_visible_stem ()->hpos_f () - x0;
418 Real stem_y = (dy && dx ? (s->hpos_f () - x0) / dx * dy : 0) + y;
421 Direction dir = directional_element(this).get ();
422 Direction sdir = directional_element (s).get ();
428 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
430 Staff_symbol_referencer_interface me (s);
431 Staff_symbol_referencer_interface last (last_visible_stem ());
433 // huh, why not for first visible?
434 if (//(s != first_visible_stem ()) &&
435 me.staff_symbol_l () != last.staff_symbol_l ())
436 stem_y += directional_element (this).get ()
437 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
443 Beam::check_stem_length_f (Real y, Real dy) const
447 Direction dir = directional_element (this).get ();
449 for (int i=0; i < stem_count (); i++)
452 if (s->invisible_b ())
455 Real stem_y = calc_stem_y_f (s, y, dy);
458 Stem_info info = s->calc_stem_info ();
460 // if (0 > info.maxy_f_ - stem_y)
461 shorten = shorten <? info.maxy_f_ - stem_y;
462 // if (0 < info.miny_f_ - stem_y)
463 lengthen = lengthen >? info.miny_f_ - stem_y;
466 if (lengthen && shorten)
467 warning (_ ("weird beam vertical offset"));
469 /* when all stems are too short, normal stems win */
470 return dir * ((shorten) ? shorten : lengthen);
474 Hmm. At this time, beam position and slope are determined. Maybe,
475 stem directions and length should set to relative to the chord's
476 position of the beam. */
478 Beam::set_stem_length (Real y, Real dy)
480 Staff_symbol_referencer_interface st (this);
481 Real half_space = st.staff_space ()/2;
482 for (int i=0; i < stem_count (); i++)
485 if (s->invisible_b ())
488 Real stem_y = calc_stem_y_f (s, y, dy);
490 /* caution: stem measures in staff-positions */
491 s->set_stemend ((stem_y + calc_interstaff_dist (s, this)) / half_space);
496 [Ross] (simplification of)
497 Set dy complying with:
499 - thick / 2 + staffline_f / 2
500 - thick + staffline_f
504 Beam::quantise_dy_f (Real dy) const
506 SCM quants = ly_eval_str ("beam-height-quants");
509 scm_to_array (quants, &a);
513 Staff_symbol_referencer_interface st (this);
514 Real staff_space = st.staff_space ();
516 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
517 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
521 return q * sign (dy);
525 Prevent interference from stafflines and beams.
526 See Documentation/tex/fonts.doc
528 We only need to quantise the (left) y-position of the beam,
529 since dy is quantised too.
530 if extend_b then stems must *not* get shorter
533 Beam::quantise_y_f (Real y, Real dy, int quant_dir)
535 int multiplicity = get_multiplicity ();
536 Staff_symbol_referencer_interface st (this);
537 Real staff_space = st.staff_space ();
538 SCM quants = scm_eval (gh_list (
539 ly_symbol2scm ("beam-vertical-position-quants"),
540 gh_int2scm (multiplicity),
541 gh_double2scm (dy/staff_space),
544 scm_to_array (quants, &a);
548 Real up_y = directional_element (this).get () * y;
549 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
551 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
552 ? iv[SMALLER] : iv[BIGGER];
554 q = iv[(Direction)quant_dir];
556 return q * directional_element (this).get ();
560 Beam::set_beaming (Beaming_info_list *beaming)
563 for (int i=0; i < stem_count (); i++)
567 if (stem (i)->beam_count (d) == 0)
568 stem (i)->set_beaming ( beaming->infos_.elem (i).beams_i_drul_[d],d);
570 while (flip (&d) != LEFT);
577 beams to go with one stem.
583 Beam::stem_beams (Stem *here, Stem *next, Stem *prev) const
585 if ((next && !(next->hpos_f () > here->hpos_f ())) ||
586 (prev && !(prev->hpos_f () < here->hpos_f ())))
587 programming_error ("Beams are not left-to-right");
589 Real staffline_f = paper_l ()->get_var ("stafflinethickness");
590 int multiplicity = get_multiplicity ();
593 Real interbeam_f = paper_l ()->interbeam_f (multiplicity);
594 Real thick = gh_scm2double (get_elt_property ("beam-thickness"));;
596 Real bdy = interbeam_f;
597 Real stemdx = staffline_f;
599 Real dx = last_visible_stem ()->hpos_f () - first_visible_stem ()->hpos_f ();
600 Real dy = get_real ("height");
601 Real dydx = dy && dx ? dy/dx : 0;
608 if (!here->first_head ())
610 else if (here->type_i ()== 1)
611 nw_f = paper_l ()->get_var ("wholewidth");
612 else if (here->type_i () == 2)
613 nw_f = paper_l ()->get_var ("notewidth") * 0.8;
615 nw_f = paper_l ()->get_var ("quartwidth");
618 Direction dir = directional_element (this).get ();
620 /* half beams extending to the left. */
623 int lhalfs= lhalfs = here->beam_count (LEFT) - prev->beam_count (RIGHT);
624 int lwholebeams= here->beam_count (LEFT) <? prev->beam_count (RIGHT) ;
626 Half beam should be one note-width,
627 but let's make sure two half-beams never touch
629 Real w = here->hpos_f () - prev->hpos_f ();
632 if (lhalfs) // generates warnings if not
633 a = lookup_l ()->beam (dydx, w, thick);
634 a.translate (Offset (-w, -w * dydx));
635 for (int j = 0; j < lhalfs; j++)
638 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
639 leftbeams.add_molecule (b);
645 int rhalfs = here->beam_count (RIGHT) - next->beam_count (LEFT);
646 int rwholebeams= here->beam_count (RIGHT) <? next->beam_count (LEFT) ;
648 Real w = next->hpos_f () - here->hpos_f ();
649 Molecule a = lookup_l ()->beam (dydx, w + stemdx, thick);
650 a.translate_axis( - stemdx/2, X_AXIS);
654 SCM gap = get_elt_property ("beam-gap");
655 if (gh_number_p (gap))
657 int gap_i = gh_scm2int ( (gap));
658 int nogap = rwholebeams - gap_i;
660 for (; j < nogap; j++)
663 b.translate_axis (-dir * bdy * j, Y_AXIS);
664 rightbeams.add_molecule (b);
666 // TODO: notehead widths differ for different types
669 a = lookup_l ()->beam (dydx, w + stemdx, thick);
672 for (; j < rwholebeams; j++)
675 b.translate (Offset (here->invisible_b () ? 0 : gap_f, -dir * bdy * j));
676 rightbeams.add_molecule (b);
681 a = lookup_l ()->beam (dydx, w, thick);
683 for (; j < rwholebeams + rhalfs; j++)
686 b.translate_axis (- dir * bdy * j, Y_AXIS);
687 rightbeams.add_molecule (b);
691 leftbeams.add_molecule (rightbeams);
694 Does beam quanting think of the asymetry of beams?
695 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
702 Beam::do_brew_molecule_p () const
704 Molecule *mol_p = new Molecule;
708 Real x0 = first_visible_stem ()->hpos_f ();
709 Real dx = last_visible_stem ()->hpos_f () - x0;
710 Real dy = get_real ("height");
711 Real dydx = dy && dx ? dy/dx : 0;
712 Real y = get_real ("y-position");
713 for (int j=0; j <stem_count (); j++)
716 Stem * prev = (j > 0)? stem (j-1) : 0;
717 Stem * next = (j < stem_count ()-1) ? stem (j+1) :0;
719 Molecule sb = stem_beams (i, next, prev);
720 Real x = i->hpos_f ()-x0;
721 sb.translate (Offset (x, x * dydx + y));
722 mol_p->add_molecule (sb);
724 mol_p->translate_axis (x0
725 - spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS), X_AXIS);
731 Beam::forced_stem_count () const
734 for (int i=0; i < stem_count (); i++)
738 if (s->invisible_b ())
741 if (((int)s->chord_start_f ())
742 && (s->get_direction () != s->get_default_dir ()))
751 TODO: Fix this class. This is wildly inefficient.
752 And it sux. Yet another array/list 'interface'.
755 Beam::stem (int i) const
757 return Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[i];
761 Beam::stem_count () const
763 Group_interface gi (this, "stems");
768 Beam::stem_top () const
770 SCM s = get_elt_property ("stems");
772 return gh_pair_p (s) ? dynamic_cast<Stem*> (unsmob_element (gh_car (s))) : 0;
774 //Group_interface__extract_elements ((Beam*) this, (Stem*) 0, "stems")[stem_count () - 1];
779 Beam::visible_stem_count () const
782 for (int i = 0; i < stem_count (); i++)
784 if (!stem (i)->invisible_b ())
791 Beam::first_visible_stem () const
793 for (int i = 0; i < stem_count (); i++)
796 if (!s->invisible_b ())
806 Beam::last_visible_stem () const
808 for (int i = stem_count (); i > 0; i--)
810 Stem* s = stem (i - 1);
811 if (!s->invisible_b ())