2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
14 -* shorter! (now +- 1000 lines)
18 -* Remove #'direction from beam. The beam has no direction per se.
19 It may only set directions for stems.
23 #include <math.h> // tanh.
25 #include "molecule.hh"
26 #include "directional-element-interface.hh"
30 #include "least-squares.hh"
32 #include "paper-def.hh"
34 #include "group-interface.hh"
35 #include "staff-symbol-referencer.hh"
41 Beam::add_stem (Grob*me, Grob*s)
43 Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
45 s->add_dependency (me);
47 assert (!Stem::beam_l (s));
48 s->set_grob_property ("beam", me->self_scm ());
50 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (s));
54 Beam::get_multiplicity (Grob*me)
57 for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = ly_cdr (s))
59 Grob * sc = unsmob_grob (ly_car (s));
61 if (Stem::has_interface (sc))
62 m = m >? Stem::beam_count (sc,LEFT) >? Stem::beam_count (sc,RIGHT);
68 After pre-processing all directions should be set.
69 Several post-processing routines (stem, slur, script) need stem/beam
71 Currenly, this means that beam has set all stem's directions.
72 [Alternatively, stems could set its own directions, according to
73 their beam, during 'final-pre-processing'.]
75 MAKE_SCHEME_CALLBACK (Beam,before_line_breaking,1);
77 Beam::before_line_breaking (SCM smob)
79 Grob * me = unsmob_grob (smob);
82 Beams with less than 2 two stems don't make much sense, but could happen
87 For a beam that only has one stem, we try to do some disappearance magic:
88 we revert the flag, and move on to The Eternal Engraving Fields.*/
91 if (visible_stem_count (me) < 2)
93 warning (_ ("beam has less than two visible stems"));
95 SCM stems = me->get_grob_property ("stems");
96 if (scm_ilength (stems) == 1)
98 warning (_("Beam has less than two stems. Removing beam."));
100 unsmob_grob (gh_car (stems))->remove_grob_property ("beam");
103 return SCM_UNSPECIFIED;
105 else if (scm_ilength (stems) == 0)
108 return SCM_UNSPECIFIED;
111 if (visible_stem_count (me) >= 1)
113 if (!Directional_element_interface::get (me))
114 Directional_element_interface::set (me, get_default_dir (me));
116 consider_auto_knees (me);
117 set_stem_directions (me);
118 set_stem_shorten (me);
124 Beam::get_default_dir (Grob*me)
126 Drul_array<int> total;
127 total[UP] = total[DOWN] = 0;
128 Drul_array<int> count;
129 count[UP] = count[DOWN] = 0;
132 Link_array<Item> stems=
133 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
135 for (int i=0; i <stems.size (); i++)
138 Direction sd = Directional_element_interface::get (s);
139 int current = sd ? (1 + d * sd)/2
140 : Stem::get_center_distance (s, (Direction)-d);
148 } while (flip (&d) != DOWN);
150 SCM func = me->get_grob_property ("dir-function");
151 SCM s = gh_call2 (func,
152 gh_cons (gh_int2scm (count[UP]),
153 gh_int2scm (count[DOWN])),
154 gh_cons (gh_int2scm (total[UP]),
155 gh_int2scm (total[DOWN])));
157 if (gh_number_p (s) && gh_scm2int (s))
161 If dir is not determined: get default
163 return to_dir (me->get_grob_property ("neutral-direction"));
168 Set all stems with non-forced direction to beam direction.
169 Urg: non-forced should become `without/with unforced' direction,
170 once stem gets cleaned-up.
173 Beam::set_stem_directions (Grob*me)
175 Link_array<Item> stems
176 =Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
177 Direction d = Directional_element_interface::get (me);
179 for (int i=0; i <stems.size (); i++)
182 SCM force = s->remove_grob_property ("dir-forced");
183 if (!gh_boolean_p (force) || !gh_scm2bool (force))
184 Directional_element_interface ::set (s,d);
189 Simplistic auto-knees; only consider vertical gap between two
192 `Forced' stem directions are ignored. If you don't want auto-knees,
193 don't set, or unset auto-knee-gap.
196 Beam::consider_auto_knees (Grob *me)
198 SCM scm = me->get_grob_property ("auto-knee-gap");
200 if (gh_number_p (scm))
204 Real staff_space = Staff_symbol_referencer::staff_space (me);
205 Real gap = gh_scm2double (scm) / staff_space;
207 Direction d = Directional_element_interface::get (me);
208 Link_array<Item> stems=
209 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
211 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
212 for (int i=1; i < stems.size (); i++)
213 if (!Stem::invisible_b (stems[i]))
214 common = common->common_refpoint (stems[i], Y_AXIS);
217 for (int i=1; i < stems.size (); i++)
219 if (!Stem::invisible_b (stems[i-1]))
221 if (Stem::invisible_b (stems[l]))
223 if (Stem::invisible_b (stems[i]))
226 Real left = Stem::extremal_heads (stems[l])[d]
227 ->relative_coordinate (common, Y_AXIS);
228 Real right = Stem::extremal_heads (stems[i])[-d]
229 ->relative_coordinate (common, Y_AXIS);
231 Real dy = right - left;
235 knee_y = (right + left) / 2;
243 for (int i=0; i < stems.size (); i++)
245 if (Stem::invisible_b (stems[i]))
248 Real y = Stem::extremal_heads (stems[i])[d]
249 ->relative_coordinate (common, Y_AXIS);
251 Directional_element_interface::set (s, y < knee_y ? UP : DOWN);
252 s->set_grob_property ("dir-forced", SCM_BOOL_T);
259 Set stem's shorten property if unset.
261 take some y-position (chord/beam/nearest?) into account
262 scmify forced-fraction
265 Beam::set_stem_shorten (Grob*m)
267 Spanner*me = dynamic_cast<Spanner*> (m);
269 Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
270 if (forced_fraction < 0.5)
273 int multiplicity = get_multiplicity (me);
275 SCM shorten = me->get_grob_property ("beamed-stem-shorten");
276 if (shorten == SCM_EOL)
279 int sz = scm_ilength (shorten);
281 Real staff_space = Staff_symbol_referencer::staff_space (me);
282 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
283 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
285 /* cute, but who invented me -- how to customise ? */
286 if (forced_fraction < 1)
289 Link_array<Item> stems=
290 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
292 for (int i=0; i < stems.size (); i++)
295 if (Stem::invisible_b (s))
297 if (gh_number_p (s->get_grob_property ("shorten")))
298 s->set_grob_property ("shorten", gh_double2scm (shorten_f));
303 Call list of y-dy-callbacks, that handle setting of
304 grob-properties y, dy.
306 User may set grob-properties: y-position-hs and height-hs
307 (to be fixed) that override the calculated y and dy.
309 Because y and dy cannot be calculated and quanted separately, we
310 always calculate both, then check for user override.
312 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
314 Beam::after_line_breaking (SCM smob)
316 Grob * me = unsmob_grob (smob);
318 me->set_grob_property ("y", gh_double2scm (0));
319 me->set_grob_property ("dy", gh_double2scm (0));
321 /* Hmm, callbacks should be called by, a eh, callback mechanism
322 somewhere (?), I guess, not by looping here. */
324 SCM list = me->get_grob_property ("y-dy-callbacks");
325 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
326 gh_call1 (ly_car (i), smob);
328 // UGH. Y is not in staff position unit?
329 // Ik dacht datwe daar juist van weg wilden?
331 // Hmm, nu hebben we 3 dimensies, want inmiddels zijn we daar
332 // weer terug, maar dan / 2
333 // (staff-space iso staff-position)
335 set_stem_lengths (me);
337 return SCM_UNSPECIFIED;
341 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
343 Beam::least_squares (SCM smob)
345 Grob *me = unsmob_grob (smob);
347 if (visible_stem_count (me) <= 1)
348 return SCM_UNSPECIFIED;
353 /* Stem_info, and thus y,dy in this function are corrected for beam-dir */
354 Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
355 if (first_ideal == Stem::calc_stem_info (last_visible_stem (me)).idealy_f_)
362 Array<Offset> ideals;
364 // ugh -> use commonx
365 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
366 Link_array<Item> stems=
367 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
369 for (int i=0; i < stems.size (); i++)
372 if (Stem::invisible_b (s))
374 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
375 Stem::calc_stem_info (s).idealy_f_));
378 minimise_least_squares (&dydx, &y, ideals);
380 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
384 /* Store true, not dir-corrected values */
385 Direction dir = Directional_element_interface::get (me);
386 me->set_grob_property ("y", gh_double2scm (y * dir));
387 me->set_grob_property ("dy", gh_double2scm (dy * dir));
388 return SCM_UNSPECIFIED;
391 MAKE_SCHEME_CALLBACK (Beam, cancel_suspect_slope, 1);
393 Beam::cancel_suspect_slope (SCM smob)
395 Grob *me = unsmob_grob (smob);
397 if (visible_stem_count (me) <= 1)
398 return SCM_UNSPECIFIED;
400 /* Stem_info, and thus y,dy in this function are corrected for beam-dir */
401 Direction dir = Directional_element_interface::get (me);
402 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
403 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
405 /* steep slope running against lengthened stem is suspect */
406 Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
407 Real last_ideal = Stem::calc_stem_info (last_visible_stem (me)).idealy_f_;
408 Real lengthened = gh_scm2double (me->get_grob_property ("outer-stem-length-limit"));
409 Real steep = gh_scm2double (me->get_grob_property ("slope-limit"));
411 // ugh -> use commonx
412 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
413 Real dydx = dy && dx ? dy/dx : 0;
415 if (( (y - first_ideal > lengthened) && (dydx > steep))
416 || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
418 Real adjusted_y = y + dy / 2;
419 /* Store true, not dir-corrected values */
420 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
421 me->set_grob_property ("dy", gh_double2scm (0));
423 return SCM_UNSPECIFIED;
427 This neat trick is by Werner Lemberg,
428 damped = tanh (slope)
429 corresponds with some tables in [Wanske]
431 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
433 Beam::slope_damping (SCM smob)
435 Grob *me = unsmob_grob (smob);
437 if (visible_stem_count (me) <= 1)
438 return SCM_UNSPECIFIED;
440 SCM s = me->get_grob_property ("damping");
441 int damping = gh_scm2int (s);
445 /* y,dy in this function are corrected for beam-dir */
446 Direction dir = Directional_element_interface::get (me);
447 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
448 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
450 // ugh -> use commonx
451 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
452 - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
453 Real dydx = dy && dx ? dy/dx : 0;
454 dydx = 0.6 * tanh (dydx) / damping;
456 Real damped_dy = dydx * dx;
457 Real adjusted_y = y + (dy - damped_dy) / 2;
458 /* Store true, not dir-corrected values */
459 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
460 me->set_grob_property ("dy", gh_double2scm (damped_dy * dir));
462 return SCM_UNSPECIFIED;
466 Quantise dy (height) of beam.
467 Generalisation of [Ross].
469 MAKE_SCHEME_CALLBACK (Beam, quantise_dy, 1);
471 Beam::quantise_dy (SCM smob)
473 Grob *me = unsmob_grob (smob);
475 if (visible_stem_count (me) <= 1)
476 return SCM_UNSPECIFIED;
479 SCM proc = me->get_grob_property ("height-quants");
480 SCM quants = gh_call2 (proc, me->self_scm (),
481 gh_double2scm (me->paper_l ()->get_var ("stafflinethickness")
484 for (SCM s = quants; gh_pair_p (s); s = ly_cdr (s))
485 a.push (gh_scm2double (ly_car (s)));
489 /* y,dy in this function are corrected for beam-dir */
490 Direction dir = Directional_element_interface::get (me);
491 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
492 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
494 Real staff_space = Staff_symbol_referencer::staff_space (me);
496 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
497 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
501 Real quantised_dy = q * sign (dy);
502 Real adjusted_y = y + (dy - quantised_dy) / 2;
503 /* Store true, not dir-corrected values */
504 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
505 me->set_grob_property ("dy", gh_double2scm (quantised_dy * dir));
507 return SCM_UNSPECIFIED;
510 /* It's tricky to have the user override y,dy directly, so we use this
511 translation func. Also, if our staff_space != 1 (smaller staff, eg),
512 user will expect staff-position to be discrete values. */
513 MAKE_SCHEME_CALLBACK (Beam, user_override, 1);
515 Beam::user_override (SCM smob)
517 Grob *me = unsmob_grob (smob);
518 Real staff_space = Staff_symbol_referencer::staff_space (me);
520 SCM s = me->get_grob_property ("staff-position");
523 Real y = gh_scm2double (s) * staff_space * 0.5;
524 me->set_grob_property ("y", gh_double2scm (y));
527 /* Name suggestions? Tilt, slope, vertical-* ? */
528 s = me->get_grob_property ("height");
531 Real dy = gh_scm2double (s) * staff_space * 0.5;
532 me->set_grob_property ("dy", gh_double2scm (dy));
535 return SCM_UNSPECIFIED;
539 Ugh, this must be last, after user_override
540 Assumes directionised y/dy.
542 MAKE_SCHEME_CALLBACK (Beam, do_quantise_y, 1);
544 Beam::do_quantise_y (SCM smob)
546 Grob *me = unsmob_grob (smob);
549 If the user set y-position, we shouldn't do quanting.
551 if (gh_number_p (me->get_grob_property ("y-position-hs")))
552 return SCM_UNSPECIFIED;
554 Real y = gh_scm2double (me->get_grob_property ("y"));
555 Real dy = gh_scm2double (me->get_grob_property ("dy"));
557 /* we can modify y, so we should quantise y */
558 Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
559 Real y_shift = check_stem_length_f (me, y, dy);
561 y = quantise_y_f (me, y, dy, 0);
564 Hmm, this is a bit keyhole operation: we're passing `this' as a
565 parameter, and member vars as SCM properties. We should decide on
566 SCM/C/C++ boundary */
567 me->set_grob_property ("y", gh_double2scm (y));
568 set_stem_lengths (me);
569 y = gh_scm2double (me->get_grob_property ("y"));
571 y_shift = check_stem_length_f (me, y, dy);
573 if (y_shift > half_space / 4)
578 for significantly lengthened or shortened stems,
579 request quanting the other way.
582 if (abs (y_shift) > half_space / 2)
583 quant_dir = sign (y_shift) * Directional_element_interface::get (me);
584 y = quantise_y_f (me, y, dy, quant_dir);
587 me->set_grob_property ("y", gh_double2scm (y));
588 // me->set_grob_property ("dy", gh_double2scm (dy));
589 return SCM_UNSPECIFIED;
594 Beam::calc_stem_y_f (Grob*me,Item* s, Real y, Real dy)
596 int beam_multiplicity = get_multiplicity (me);
597 int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
599 SCM space_proc = me->get_grob_property ("space-function");
600 SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
602 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
603 Real interbeam_f = gh_scm2double (space) ;
605 // ugh -> use commonx
606 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
607 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
608 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
611 Direction dir = Directional_element_interface::get (me);
612 Direction sdir = Directional_element_interface::get (s);
618 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
622 // huh, why not for first visible?
623 if (Staff_symbol_referencer::staff_symbol_l (s)
624 != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
625 stem_y += Directional_element_interface::get (me)
626 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
633 Beam::check_stem_length_f (Grob*me,Real y, Real dy)
637 Direction dir = Directional_element_interface::get (me);
639 Link_array<Item> stems=
640 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
642 for (int i=0; i < stems.size (); i++)
645 if (Stem::invisible_b (s))
648 Real stem_y = calc_stem_y_f (me, s, y, dy);
651 Stem_info info = Stem::calc_stem_info (s);
653 // if (0 > info.maxy_f_ - stem_y)
654 shorten = shorten <? info.maxy_f_ - stem_y;
655 // if (0 < info.miny_f_ - stem_y)
656 lengthen = lengthen >? info.miny_f_ - stem_y;
659 if (lengthen && shorten)
660 warning (_ ("weird beam vertical offset"));
662 /* when all stems are too short, normal stems win */
663 return dir * ((shorten) ? shorten : lengthen);
667 Hmm. At this time, beam position and slope are determined. Maybe,
668 stem directions and length should set to relative to the chord's
669 position of the beam. */
671 Beam::set_stem_lengths (Grob *me)
673 if (visible_stem_count (me) <= 1)
676 Real y = gh_scm2double (me->get_grob_property ("y"));
677 Real dy = gh_scm2double (me->get_grob_property ("dy"));
679 Real half_space = Staff_symbol_referencer::staff_space (me)/2;
680 Link_array<Item> stems=
681 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
683 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
684 for (int i=1; i < stems.size (); i++)
685 if (!Stem::invisible_b (stems[i]))
686 common = common->common_refpoint (stems[i], Y_AXIS);
688 for (int i=0; i < stems.size (); i++)
691 if (Stem::invisible_b (s))
694 Real stem_y = calc_stem_y_f (me, s, y, dy);
696 // doesn't play well with dvips
697 if (scm_definedp (ly_symbol2scm ("ps-testing"), SCM_UNDEFINED)
699 if (Stem::get_direction (s) == Directional_element_interface::get (me))
700 stem_y += Stem::get_direction (s)
701 * gh_scm2double (me->get_grob_property ("thickness")) / 2;
703 /* caution: stem measures in staff-positions */
704 Real id = me->relative_coordinate (common, Y_AXIS)
705 - stems[i]->relative_coordinate (common, Y_AXIS);
706 Stem::set_stemend (s, (stem_y + id) / half_space);
711 Prevent interference from stafflines and beams.
713 We only need to quantise the (left) y of the beam,
714 since dy is quantised too.
715 if extend_b then stems must *not* get shorter
718 Beam::quantise_y_f (Grob*me,Real y, Real dy, int quant_dir)
720 int multiplicity = get_multiplicity (me);
722 Real staff_space = Staff_symbol_referencer::staff_space (me);
723 Real thick = me->paper_l ()->get_var ("stafflinethickness");
726 SCM proc = me->get_grob_property ("vertical-position-quant-function");
727 SCM quants = scm_apply (proc,
729 scm_list_n (gh_int2scm (multiplicity),
730 gh_double2scm (dy/staff_space),
731 gh_double2scm (thick/staff_space),
732 SCM_EOL, SCM_UNDEFINED));
736 for (; gh_pair_p (quants); quants = ly_cdr (quants))
737 a.push (gh_scm2double (ly_car (quants)));
742 Real up_y = Directional_element_interface::get (me) * y;
743 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
745 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
746 ? iv[SMALLER] : iv[BIGGER];
748 q = iv[ (Direction)quant_dir];
750 return q * Directional_element_interface::get (me);
754 Beam::set_beaming (Grob*me,Beaming_info_list *beaming)
756 Link_array<Grob> stems=
757 Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
760 for (int i=0; i < stems.size (); i++)
764 /* Don't overwrite user override (?) */
765 if (Stem::beam_count (stems[i], d) == 0
766 /* Don't set beaming for outside of outer stems */
767 && ! (d == LEFT && i == 0)
768 && ! (d == RIGHT && i == stems.size () -1))
770 int b = beaming->infos_.elem (i).beams_i_drul_[d];
771 Stem::set_beaming (stems[i], b, d);
774 while (flip (&d) != LEFT);
781 beams to go with one stem.
786 Beam::stem_beams (Grob*me,Item *here, Item *next, Item *prev,
787 Real /* dy */ , Real dydx
790 // ugh -> use commonx
791 if ((next && ! (next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
792 (prev && ! (prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
793 programming_error ("Beams are not left-to-right");
795 int multiplicity = get_multiplicity (me);
797 SCM space_proc = me->get_grob_property ("space-function");
798 SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
800 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
801 Real interbeam_f = gh_scm2double (space) ;
803 Real bdy = interbeam_f;
806 // ugh -> use commonx
807 Real dx = visible_stem_count (me) ?
808 last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS)
816 if (!Stem::first_head (here))
819 int t = Stem::type_i (here);
821 SCM proc = me->get_grob_property ("flag-width-function");
822 SCM result = gh_call1 (proc, gh_int2scm (t));
823 nw_f = gh_scm2double (result);
827 Direction dir = Directional_element_interface::get (me);
829 /* [Tremolo] beams on whole notes may not have direction set? */
831 dir = Directional_element_interface::get (here);
834 /* half beams extending to the left. */
837 int lhalfs= lhalfs = Stem::beam_count (here,LEFT) - Stem::beam_count (prev,RIGHT);
838 int lwholebeams= Stem::beam_count (here,LEFT) <? Stem::beam_count (prev,RIGHT) ;
840 Half beam should be one note-width,
841 but let's make sure two half-beams never touch
844 // FIXME: TODO (check) stem width / sloped beams
845 Real w = here->relative_coordinate (0, X_AXIS)
846 - prev->relative_coordinate (0, X_AXIS);
847 Real stem_w = gh_scm2double (prev->get_grob_property ("thickness"))
849 * me->paper_l ()->get_var ("stafflinethickness");
853 if (lhalfs) // generates warnings if not
854 a = Lookup::beam (dydx, w + stem_w, thick);
855 a.translate (Offset (-w, -w * dydx));
856 a.translate_axis (-stem_w/2, X_AXIS);
857 for (int j = 0; j < lhalfs; j++)
860 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
861 leftbeams.add_molecule (b);
867 int rhalfs = Stem::beam_count (here,RIGHT)
868 - Stem::beam_count (next,LEFT);
869 int rwholebeams= Stem::beam_count (here,RIGHT)
870 <? Stem::beam_count (next,LEFT) ;
872 Real w = next->relative_coordinate (0, X_AXIS)
873 - here->relative_coordinate (0, X_AXIS);
875 Real stem_w = gh_scm2double (next->get_grob_property ("thickness"))
877 * me->paper_l ()->get_var ("stafflinethickness");
879 Molecule a = Lookup::beam (dydx, w + stem_w, thick);
880 a.translate_axis (- stem_w/2, X_AXIS);
884 SCM gap = me->get_grob_property ("gap");
885 if (gh_number_p (gap))
887 int gap_i = gh_scm2int ((gap));
888 int nogap = rwholebeams - gap_i;
890 for (; j < nogap; j++)
893 b.translate_axis (-dir * bdy * j, Y_AXIS);
894 rightbeams.add_molecule (b);
896 if (Stem::invisible_b (here))
901 a = Lookup::beam (dydx, w + stem_w, thick);
904 for (; j < rwholebeams; j++)
908 if (Stem::invisible_b (here))
909 // ugh, see chord-tremolo.ly
910 tx = (-dir + 1) / 2 * nw_f * 1.5 + gap_f/4;
913 b.translate (Offset (tx, -dir * bdy * j));
914 rightbeams.add_molecule (b);
919 a = Lookup::beam (dydx, w, thick);
921 for (; j < rwholebeams + rhalfs; j++)
924 b.translate_axis (- dir * bdy * j, Y_AXIS);
925 rightbeams.add_molecule (b);
929 leftbeams.add_molecule (rightbeams);
932 Does beam quanting think of the asymetry of beams?
933 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
938 MAKE_SCHEME_CALLBACK (Beam,brew_molecule,1);
940 Beam::brew_molecule (SCM smob)
942 Grob * me =unsmob_grob (smob);
945 if (!gh_pair_p (me->get_grob_property ("stems")))
948 Link_array<Item>stems =
949 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
950 if (visible_stem_count (me))
952 // ugh -> use commonx
953 x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
954 dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
958 x0 = stems[0]->relative_coordinate (0, X_AXIS);
959 dx = stems.top ()->relative_coordinate (0, X_AXIS) - x0;
965 TODO: the naming of the grob properties sucks.
967 SCM dy_s = me->get_grob_property ("dy");
968 SCM y_s = me->get_grob_property ("y");
971 Real dy = gh_number_p (dy_s) ? gh_scm2double (dy_s) : 0.0;
972 Real dydx = dy && dx ? dy/dx : 0;
973 Real y = gh_number_p (y_s) ? gh_scm2double (y_s) : 0.0;
976 for (int j=0; j <stems.size (); j++)
979 Item * prev = (j > 0)? stems[j-1] : 0;
980 Item * next = (j < stems.size ()-1) ? stems[j+1] :0;
982 Molecule sb = stem_beams (me, i, next, prev, dy, dydx);
983 Real x = i->relative_coordinate (0, X_AXIS)-x0;
984 sb.translate (Offset (x, x * dydx + y));
985 mol.add_molecule (sb);
987 mol.translate_axis (x0
988 - dynamic_cast<Spanner*> (me)->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
990 return mol.smobbed_copy ();
994 Beam::forced_stem_count (Grob*me)
996 Link_array<Item>stems =
997 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
999 for (int i=0; i < stems.size (); i++)
1003 if (Stem::invisible_b (s))
1006 if (( (int)Stem::chord_start_f (s))
1007 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1017 use filter and standard list functions.
1020 Beam::visible_stem_count (Grob*me)
1022 Link_array<Item>stems =
1023 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1025 for (int i = stems.size (); i--;)
1027 if (!Stem::invisible_b (stems[i]))
1034 Beam::first_visible_stem (Grob*me)
1036 Link_array<Item>stems =
1037 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1039 for (int i = 0; i < stems.size (); i++)
1041 if (!Stem::invisible_b (stems[i]))
1048 Beam::last_visible_stem (Grob*me)
1050 Link_array<Item>stems =
1051 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1052 for (int i = stems.size (); i--;)
1054 if (!Stem::invisible_b (stems[i]))
1063 handle rest under beam (do_post: beams are calculated now)
1064 what about combination of collisions and rest under beam.
1068 rest -> stem -> beam -> interpolate_y_position ()
1070 MAKE_SCHEME_CALLBACK (Beam,rest_collision_callback,2);
1072 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1074 Grob *rest = unsmob_grob (element_smob);
1075 Axis a = (Axis) gh_scm2int (axis);
1077 assert (a == Y_AXIS);
1079 Grob * st = unsmob_grob (rest->get_grob_property ("stem"));
1082 return gh_double2scm (0.0);
1083 Grob * beam = unsmob_grob (stem->get_grob_property ("beam"));
1084 if (!beam || !Beam::has_interface (beam) || !Beam::visible_stem_count (beam))
1085 return gh_double2scm (0.0);
1087 // make callback for rest from this.
1092 // todo: make sure this calced already.
1093 SCM s = beam->get_grob_property ("dy");
1094 if (gh_number_p (s))
1095 beam_dy = gh_scm2double (s);
1097 s = beam->get_grob_property ("y");
1098 if (gh_number_p (s))
1099 beam_y = gh_scm2double (s);
1101 // ugh -> use commonx
1102 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1103 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1104 Real dydx = beam_dy && dx ? beam_dy/dx : 0;
1106 Direction d = Stem::get_direction (stem);
1107 Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
1109 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1112 Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space ; // refp??
1115 = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance"));
1117 minimum_dist + -d * (beamy - rest_dim) >? 0;
1119 int stafflines = Staff_symbol_referencer::line_count (rest);
1121 // move discretely by half spaces.
1122 int discrete_dist = int (ceil (dist));
1124 // move by whole spaces inside the staff.
1125 if (discrete_dist < stafflines+1)
1126 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
1128 return gh_double2scm (-d * discrete_dist);
1133 Beam::has_interface (Grob*me)
1135 return me->has_interface (ly_symbol2scm ("beam-interface"));