2 beam.cc -- implement Beam
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 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 me->warning (_ ("beam has less than two visible stems"));
95 SCM stems = me->get_grob_property ("stems");
96 if (scm_ilength (stems) == 1)
98 me->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);
271 int multiplicity = get_multiplicity (me);
273 SCM shorten = me->get_grob_property ("beamed-stem-shorten");
274 if (shorten == SCM_EOL)
277 int sz = scm_ilength (shorten);
279 Real staff_space = Staff_symbol_referencer::staff_space (me);
280 SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
281 Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
283 /* your similar cute comment here */
284 shorten_f *= forced_fraction;
286 me->set_grob_property ("shorten", gh_double2scm (shorten_f));
290 Call list of y-dy-callbacks, that handle setting of
291 grob-properties y, dy.
293 User may set grob-properties: y-position-hs and height-hs
294 (to be fixed) that override the calculated y and dy.
296 Because y and dy cannot be calculated and quanted separately, we
297 always calculate both, then check for user override.
299 MAKE_SCHEME_CALLBACK (Beam, after_line_breaking, 1);
301 Beam::after_line_breaking (SCM smob)
303 Grob * me = unsmob_grob (smob);
305 me->set_grob_property ("y", gh_double2scm (0));
306 me->set_grob_property ("dy", gh_double2scm (0));
308 /* Hmm, callbacks should be called by, a eh, callback mechanism
309 somewhere (?), I guess, not by looping here. */
311 SCM list = me->get_grob_property ("y-dy-callbacks");
312 for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
313 gh_call1 (ly_car (i), smob);
315 // UGH. Y is not in staff position unit?
316 // Ik dacht datwe daar juist van weg wilden?
318 // Hmm, nu hebben we 3 dimensies, want inmiddels zijn we daar
319 // weer terug, maar dan / 2
320 // (staff-space iso staff-position)
322 set_stem_lengths (me);
324 return SCM_UNSPECIFIED;
328 MAKE_SCHEME_CALLBACK (Beam, least_squares, 1);
330 Beam::least_squares (SCM smob)
332 Grob *me = unsmob_grob (smob);
334 int count = visible_stem_count (me);
336 return SCM_UNSPECIFIED;
340 Direction dir = Directional_element_interface::get (me);
342 /* Stem_info, and thus y,dy in this function are corrected for beam-dir */
343 Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
344 if (first_ideal == Stem::calc_stem_info (last_visible_stem (me)).idealy_f_)
346 Real left = Stem::chord_start_f (first_visible_stem (me));
347 Real right = Stem::chord_start_f (last_visible_stem (me));
349 /* Make simple beam on middle line have small tilt */
350 if (!first_ideal && left != right && count == 2)
352 int d = sign (right - left) * dir;
353 dy = gh_scm2double (me->get_grob_property ("thickness")) * d;
364 Array<Offset> ideals;
366 // ugh -> use commonx
367 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
368 Link_array<Item> stems=
369 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
371 for (int i=0; i < stems.size (); i++)
374 if (Stem::invisible_b (s))
376 ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0,
377 Stem::calc_stem_info (s).idealy_f_));
380 minimise_least_squares (&dydx, &y, ideals);
382 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
386 /* Store true, not dir-corrected values */
387 me->set_grob_property ("y", gh_double2scm (y * dir));
388 me->set_grob_property ("dy", gh_double2scm (dy * dir));
389 return SCM_UNSPECIFIED;
392 MAKE_SCHEME_CALLBACK (Beam, check_concave, 1);
394 Beam::check_concave (SCM smob)
396 Grob *me = unsmob_grob (smob);
398 Link_array<Item> stems =
399 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
401 for (int i = 0; i < stems.size ();)
403 if (Stem::invisible_b (stems[i]))
409 if (stems.size () < 3)
410 return SCM_UNSPECIFIED;
412 /* Concaveness try #2: Sum distances of inner noteheads that
413 fall outside the interval of the two outer noteheads */
415 Interval iv = Interval (Stem::chord_start_f (stems[0]),
416 Stem::chord_start_f (stems.top ()));
418 if (iv[MAX] < iv[MIN])
420 iv = Interval (iv[MAX], iv[MIN]);
422 for (int i = 1; i < stems.size () - 1; i++)
425 Real f = Stem::chord_start_f (stems[i]);
426 if ((c = f - iv[MAX]) > 0)
428 else if ((c = f - iv[MIN]) < 0)
431 concave *= Directional_element_interface::get (me);
433 Real concaveness = concave / (stems.size () - 2);
434 /* ugh: this is the a kludge to get input/regression/beam-concave.ly
435 to behave as baerenreiter. */
436 concaveness /= (stems.size () - 2);
438 Real r = gh_scm2double (me->get_grob_property ("concaveness-threshold"));
440 /* TODO: some sort of damping iso -> plain horizontal */
443 Direction dir = Directional_element_interface::get (me);
444 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
445 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
447 Real adjusted_y = y + dy / 2;
448 /* Store true, not dir-corrected values */
449 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
450 me->set_grob_property ("dy", gh_double2scm (0));
453 return SCM_UNSPECIFIED;
457 This neat trick is by Werner Lemberg,
458 damped = tanh (slope)
459 corresponds with some tables in [Wanske]
461 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 1);
463 Beam::slope_damping (SCM smob)
465 Grob *me = unsmob_grob (smob);
467 if (visible_stem_count (me) <= 1)
468 return SCM_UNSPECIFIED;
470 SCM s = me->get_grob_property ("damping");
471 int damping = gh_scm2int (s);
475 /* y,dy in this function are corrected for beam-dir */
476 Direction dir = Directional_element_interface::get (me);
477 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
478 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
480 // ugh -> use commonx
481 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
482 - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
483 Real dydx = dy && dx ? dy/dx : 0;
484 dydx = 0.6 * tanh (dydx) / damping;
486 Real damped_dy = dydx * dx;
487 Real adjusted_y = y + (dy - damped_dy) / 2;
488 /* Store true, not dir-corrected values */
489 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
490 me->set_grob_property ("dy", gh_double2scm (damped_dy * dir));
492 return SCM_UNSPECIFIED;
496 Quantise dy (height) of beam.
497 Generalisation of [Ross].
499 MAKE_SCHEME_CALLBACK (Beam, quantise_dy, 1);
501 Beam::quantise_dy (SCM smob)
503 Grob *me = unsmob_grob (smob);
505 if (visible_stem_count (me) <= 1)
506 return SCM_UNSPECIFIED;
509 SCM proc = me->get_grob_property ("height-quant-function");
510 SCM quants = gh_call2 (proc, me->self_scm (),
511 gh_double2scm (me->paper_l ()->get_var ("stafflinethickness")
514 for (SCM s = quants; gh_pair_p (s); s = ly_cdr (s))
515 a.push (gh_scm2double (ly_car (s)));
519 /* y,dy in this function are corrected for beam-dir */
520 Direction dir = Directional_element_interface::get (me);
521 Real y = gh_scm2double (me->get_grob_property ("y")) * dir;
522 Real dy = gh_scm2double (me->get_grob_property ("dy")) * dir;
524 Real staff_space = Staff_symbol_referencer::staff_space (me);
526 Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
529 Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
533 Real q = (!dy || iv[SMALLER] != 0) ? iv[SMALLER] : iv[BIGGER];
536 Real quantised_dy = q * (dy != 0 ? sign (dy) : 1);
537 Real adjusted_y = y + (dy - quantised_dy) * 0.5;
538 /* Store true, not dir-corrected values */
539 me->set_grob_property ("y", gh_double2scm (adjusted_y * dir));
540 me->set_grob_property ("dy", gh_double2scm (quantised_dy * dir));
542 return SCM_UNSPECIFIED;
545 /* It's tricky to have the user override y,dy directly, so we use this
546 translation func. Also, if our staff_space != 1 (smaller staff, eg),
547 user will expect staff-position to be discrete values. */
548 MAKE_SCHEME_CALLBACK (Beam, user_override, 1);
550 Beam::user_override (SCM smob)
552 Grob *me = unsmob_grob (smob);
553 Real staff_space = Staff_symbol_referencer::staff_space (me);
555 SCM s = me->get_grob_property ("staff-position");
558 Real y = gh_scm2double (s) * staff_space * 0.5;
559 me->set_grob_property ("y", gh_double2scm (y));
562 /* Name suggestions? Tilt, slope, vertical-* ? */
563 s = me->get_grob_property ("height");
566 Real dy = gh_scm2double (s) * staff_space * 0.5;
567 me->set_grob_property ("dy", gh_double2scm (dy));
570 return SCM_UNSPECIFIED;
574 Ugh, this must be last, after user_override
575 Assumes directionised y/dy.
577 MAKE_SCHEME_CALLBACK (Beam, do_quantise_y, 1);
579 Beam::do_quantise_y (SCM smob)
581 Grob *me = unsmob_grob (smob);
584 If the user set y-position, we shouldn't do quanting.
586 if (gh_number_p (me->get_grob_property ("y-position-hs")))
587 return SCM_UNSPECIFIED;
589 Real y = gh_scm2double (me->get_grob_property ("y"));
590 Real dy = gh_scm2double (me->get_grob_property ("dy"));
592 /* we can modify y, so we should quantise y */
593 Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
594 Real y_shift = check_stem_length_f (me, y, dy);
596 y = quantise_y_f (me, y, dy, 0);
599 Hmm, this is a bit keyhole operation: we're passing `this' as a
600 parameter, and member vars as SCM properties. We should decide on
601 SCM/C/C++ boundary */
602 me->set_grob_property ("y", gh_double2scm (y));
603 set_stem_lengths (me);
604 y = gh_scm2double (me->get_grob_property ("y"));
606 y_shift = check_stem_length_f (me, y, dy);
608 if (y_shift > half_space / 4)
613 for significantly lengthened or shortened stems,
614 request quanting the other way.
617 if (abs (y_shift) > half_space / 2)
618 quant_dir = sign (y_shift) * Directional_element_interface::get (me);
619 y = quantise_y_f (me, y, dy, quant_dir);
622 me->set_grob_property ("y", gh_double2scm (y));
623 // me->set_grob_property ("dy", gh_double2scm (dy));
624 return SCM_UNSPECIFIED;
629 Beam::calc_stem_y_f (Grob*me,Item* s, Real y, Real dy)
631 int beam_multiplicity = get_multiplicity (me);
632 int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
634 SCM space_proc = me->get_grob_property ("space-function");
635 SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
637 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
638 Real interbeam_f = gh_scm2double (space) ;
640 // ugh -> use commonx
641 Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
642 Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
643 Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
646 Direction dir = Directional_element_interface::get (me);
647 Direction sdir = Directional_element_interface::get (s);
653 * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
657 // huh, why not for first visible?
658 if (Staff_symbol_referencer::staff_symbol_l (s)
659 != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
660 stem_y += Directional_element_interface::get (me)
661 * (beam_multiplicity - stem_multiplicity) * interbeam_f;
667 /* Make very sure that we don't have stems that are too short.
668 Try our best not to have stems that are too long (think: knees).
670 Optionally (testing): try to lengthen more, to reach more ideal
673 Beam::check_stem_length_f (Grob *me, Real y, Real dy)
677 Direction dir = Directional_element_interface::get (me);
679 Link_array<Item> stems=
680 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
683 int ideal_lengthen_count = 0;
684 Real ideal_lengthen = 0;
685 int ideal_shorten_count = 0;
686 Real ideal_shorten = 0;
688 for (int i=0; i < stems.size (); i++)
691 if (Stem::invisible_b (s))
694 knee |= dir != Directional_element_interface::get (s);
696 Real stem_y = calc_stem_y_f (me, s, y, dy);
699 Stem_info info = Stem::calc_stem_info (s);
701 shorten = shorten <? info.maxy_f_ - stem_y;
702 lengthen = lengthen >? info.miny_f_ - stem_y;
704 if (info.idealy_f_ - stem_y > 0)
706 ideal_lengthen += info.idealy_f_ - stem_y;
707 ideal_lengthen_count++;
709 else if (info.idealy_f_ - stem_y < 0)
711 ideal_shorten += info.idealy_f_ - stem_y;
712 ideal_shorten_count++;
716 if (lengthen && shorten)
717 me->warning (_ ("weird beam vertical offset"));
719 if (ideal_lengthen_count)
720 lengthen = (ideal_lengthen / ideal_lengthen_count) >? lengthen;
721 if (knee && ideal_shorten_count)
722 shorten = (ideal_shorten / ideal_shorten_count) <? shorten;
724 if (lengthen && shorten)
725 return dir * (lengthen + shorten);
727 return dir * (shorten ? shorten : lengthen);
731 Hmm. At this time, beam position and slope are determined. Maybe,
732 stem directions and length should set to relative to the chord's
733 position of the beam. */
735 Beam::set_stem_lengths (Grob *me)
737 if (visible_stem_count (me) <= 1)
740 Real y = gh_scm2double (me->get_grob_property ("y"));
741 Real dy = gh_scm2double (me->get_grob_property ("dy"));
743 Real half_space = Staff_symbol_referencer::staff_space (me)/2;
744 Link_array<Item> stems=
745 Pointer_group_interface__extract_grobs (me, (Item*)0, "stems");
747 Grob *common = me->common_refpoint (stems[0], Y_AXIS);
748 for (int i=1; i < stems.size (); i++)
749 if (!Stem::invisible_b (stems[i]))
750 common = common->common_refpoint (stems[i], Y_AXIS);
752 for (int i=0; i < stems.size (); i++)
755 if (Stem::invisible_b (s))
758 Real stem_y = calc_stem_y_f (me, s, y, dy);
760 // doesn't play well with dvips
761 if (scm_definedp (ly_symbol2scm ("ps-testing"), SCM_UNDEFINED)
763 if (Stem::get_direction (s) == Directional_element_interface::get (me))
764 stem_y += Stem::get_direction (s)
765 * gh_scm2double (me->get_grob_property ("thickness")) / 2;
767 /* caution: stem measures in staff-positions */
768 Real id = me->relative_coordinate (common, Y_AXIS)
769 - stems[i]->relative_coordinate (common, Y_AXIS);
770 Stem::set_stemend (s, (stem_y + id) / half_space);
775 Prevent interference from stafflines and beams.
777 We only need to quantise the (left) y of the beam,
778 since dy is quantised too.
779 if extend_b then stems must *not* get shorter
782 Beam::quantise_y_f (Grob*me,Real y, Real dy, int quant_dir)
784 int multiplicity = get_multiplicity (me);
786 Real staff_space = Staff_symbol_referencer::staff_space (me);
787 Real thick = me->paper_l ()->get_var ("stafflinethickness");
790 SCM proc = me->get_grob_property ("vertical-position-quant-function");
791 SCM quants = scm_apply (proc,
793 scm_list_n (gh_int2scm (multiplicity),
794 gh_double2scm (dy/staff_space),
795 gh_double2scm (thick/staff_space),
796 SCM_EOL, SCM_UNDEFINED));
800 for (; gh_pair_p (quants); quants = ly_cdr (quants))
801 a.push (gh_scm2double (ly_car (quants)));
806 Real up_y = Directional_element_interface::get (me) * y;
807 Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
809 Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y
810 ? iv[SMALLER] : iv[BIGGER];
812 q = iv[ (Direction)quant_dir];
814 return q * Directional_element_interface::get (me);
818 Beam::set_beaming (Grob*me,Beaming_info_list *beaming)
820 Link_array<Grob> stems=
821 Pointer_group_interface__extract_grobs (me, (Grob*)0, "stems");
824 for (int i=0; i < stems.size (); i++)
828 /* Don't overwrite user override (?) */
829 if (Stem::beam_count (stems[i], d) == -1
830 /* Don't set beaming for outside of outer stems */
831 && ! (d == LEFT && i == 0)
832 && ! (d == RIGHT && i == stems.size () -1))
834 int b = beaming->infos_.elem (i).beams_i_drul_[d];
835 Stem::set_beaming (stems[i], b, d);
838 while (flip (&d) != LEFT);
845 beams to go with one stem.
850 Beam::stem_beams (Grob*me,Item *here, Item *next, Item *prev,
851 Real /* dy */ , Real dydx
854 // ugh -> use commonx
855 if ((next && ! (next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
856 (prev && ! (prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
857 programming_error ("Beams are not left-to-right");
859 int multiplicity = get_multiplicity (me);
861 SCM space_proc = me->get_grob_property ("space-function");
862 SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
864 Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
865 Real interbeam_f = gh_scm2double (space) ;
867 Real bdy = interbeam_f;
870 // ugh -> use commonx
871 Real dx = visible_stem_count (me) ?
872 last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS)
880 if (!Stem::first_head (here))
883 int t = Stem::type_i (here);
885 SCM proc = me->get_grob_property ("flag-width-function");
886 SCM result = gh_call1 (proc, gh_int2scm (t));
887 nw_f = gh_scm2double (result);
891 Direction dir = Directional_element_interface::get (me);
893 /* [Tremolo] beams on whole notes may not have direction set? */
895 dir = Directional_element_interface::get (here);
898 /* half beams extending to the left. */
901 int lhalfs= lhalfs = Stem::beam_count (here,LEFT) - Stem::beam_count (prev,RIGHT);
902 int lwholebeams= Stem::beam_count (here,LEFT) <? Stem::beam_count (prev,RIGHT) ;
904 Half beam should be one note-width,
905 but let's make sure two half-beams never touch
908 // FIXME: TODO (check) stem width / sloped beams
909 Real w = here->relative_coordinate (0, X_AXIS)
910 - prev->relative_coordinate (0, X_AXIS);
911 Real stem_w = gh_scm2double (prev->get_grob_property ("thickness"))
913 * me->paper_l ()->get_var ("stafflinethickness");
917 if (lhalfs) // generates warnings if not
918 a = Lookup::beam (dydx, w + stem_w, thick);
919 a.translate (Offset (-w, -w * dydx));
920 a.translate_axis (-stem_w/2, X_AXIS);
921 for (int j = 0; j < lhalfs; j++)
924 b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
925 leftbeams.add_molecule (b);
931 int rhalfs = Stem::beam_count (here,RIGHT)
932 - Stem::beam_count (next,LEFT);
933 int rwholebeams= Stem::beam_count (here,RIGHT)
934 <? Stem::beam_count (next,LEFT) ;
936 Real w = next->relative_coordinate (0, X_AXIS)
937 - here->relative_coordinate (0, X_AXIS);
939 Real stem_w = gh_scm2double (next->get_grob_property ("thickness"))
941 * me->paper_l ()->get_var ("stafflinethickness");
943 Molecule a = Lookup::beam (dydx, w + stem_w, thick);
944 a.translate_axis (- stem_w/2, X_AXIS);
948 SCM gap = me->get_grob_property ("gap");
949 if (gh_number_p (gap))
951 int gap_i = gh_scm2int ((gap));
952 int nogap = rwholebeams - gap_i;
954 for (; j < nogap; j++)
957 b.translate_axis (-dir * bdy * j, Y_AXIS);
958 rightbeams.add_molecule (b);
960 if (Stem::invisible_b (here))
965 a = Lookup::beam (dydx, w + stem_w, thick);
968 for (; j < rwholebeams; j++)
972 if (Stem::invisible_b (here))
973 // ugh, see chord-tremolo.ly
974 tx = (-dir + 1) / 2 * nw_f * 1.5 + gap_f/4;
977 b.translate (Offset (tx, -dir * bdy * j));
978 rightbeams.add_molecule (b);
983 a = Lookup::beam (dydx, w, thick);
985 for (; j < rwholebeams + rhalfs; j++)
988 b.translate_axis (- dir * bdy * j, Y_AXIS);
989 rightbeams.add_molecule (b);
993 leftbeams.add_molecule (rightbeams);
996 Does beam quanting think of the asymetry of beams?
997 Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
1002 MAKE_SCHEME_CALLBACK (Beam,brew_molecule,1);
1004 Beam::brew_molecule (SCM smob)
1006 Grob * me =unsmob_grob (smob);
1009 if (!gh_pair_p (me->get_grob_property ("stems")))
1012 Link_array<Item>stems =
1013 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1014 if (visible_stem_count (me))
1016 // ugh -> use commonx
1017 x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
1018 dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
1022 x0 = stems[0]->relative_coordinate (0, X_AXIS);
1023 dx = stems.top ()->relative_coordinate (0, X_AXIS) - x0;
1029 TODO: the naming of the grob properties sucks.
1031 SCM dy_s = me->get_grob_property ("dy");
1032 SCM y_s = me->get_grob_property ("y");
1035 Real dy = gh_number_p (dy_s) ? gh_scm2double (dy_s) : 0.0;
1036 Real dydx = dy && dx ? dy/dx : 0;
1037 Real y = gh_number_p (y_s) ? gh_scm2double (y_s) : 0.0;
1040 for (int j=0; j <stems.size (); j++)
1043 Item * prev = (j > 0)? stems[j-1] : 0;
1044 Item * next = (j < stems.size ()-1) ? stems[j+1] :0;
1046 Molecule sb = stem_beams (me, i, next, prev, dy, dydx);
1047 Real x = i->relative_coordinate (0, X_AXIS)-x0;
1048 sb.translate (Offset (x, x * dydx + y));
1049 mol.add_molecule (sb);
1051 mol.translate_axis (x0
1052 - dynamic_cast<Spanner*> (me)->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
1054 return mol.smobbed_copy ();
1058 Beam::forced_stem_count (Grob*me)
1060 Link_array<Item>stems =
1061 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1063 for (int i=0; i < stems.size (); i++)
1067 if (Stem::invisible_b (s))
1070 if (( (int)Stem::chord_start_f (s))
1071 && (Stem::get_direction (s) != Stem::get_default_dir (s)))
1081 use filter and standard list functions.
1084 Beam::visible_stem_count (Grob*me)
1086 Link_array<Item>stems =
1087 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1089 for (int i = stems.size (); i--;)
1091 if (!Stem::invisible_b (stems[i]))
1098 Beam::first_visible_stem (Grob*me)
1100 Link_array<Item>stems =
1101 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1103 for (int i = 0; i < stems.size (); i++)
1105 if (!Stem::invisible_b (stems[i]))
1112 Beam::last_visible_stem (Grob*me)
1114 Link_array<Item>stems =
1115 Pointer_group_interface__extract_grobs (me, (Item*) 0, "stems");
1116 for (int i = stems.size (); i--;)
1118 if (!Stem::invisible_b (stems[i]))
1127 handle rest under beam (do_post: beams are calculated now)
1128 what about combination of collisions and rest under beam.
1132 rest -> stem -> beam -> interpolate_y_position ()
1134 MAKE_SCHEME_CALLBACK (Beam,rest_collision_callback,2);
1136 Beam::rest_collision_callback (SCM element_smob, SCM axis)
1138 Grob *rest = unsmob_grob (element_smob);
1139 Axis a = (Axis) gh_scm2int (axis);
1141 assert (a == Y_AXIS);
1143 Grob * st = unsmob_grob (rest->get_grob_property ("stem"));
1146 return gh_double2scm (0.0);
1147 Grob * beam = unsmob_grob (stem->get_grob_property ("beam"));
1148 if (!beam || !Beam::has_interface (beam) || !Beam::visible_stem_count (beam))
1149 return gh_double2scm (0.0);
1151 // make callback for rest from this.
1156 // todo: make sure this calced already.
1157 SCM s = beam->get_grob_property ("dy");
1158 if (gh_number_p (s))
1159 beam_dy = gh_scm2double (s);
1161 s = beam->get_grob_property ("y");
1162 if (gh_number_p (s))
1163 beam_y = gh_scm2double (s);
1165 // ugh -> use commonx
1166 Real x0 = first_visible_stem (beam)->relative_coordinate (0, X_AXIS);
1167 Real dx = last_visible_stem (beam)->relative_coordinate (0, X_AXIS) - x0;
1168 Real dydx = beam_dy && dx ? beam_dy/dx : 0;
1170 Direction d = Stem::get_direction (stem);
1171 Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
1173 Real staff_space = Staff_symbol_referencer::staff_space (rest);
1176 Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space ; // refp??
1179 = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance"));
1181 minimum_dist + -d * (beamy - rest_dim) >? 0;
1183 int stafflines = Staff_symbol_referencer::line_count (rest);
1185 // move discretely by half spaces.
1186 int discrete_dist = int (ceil (dist));
1188 // move by whole spaces inside the staff.
1189 if (discrete_dist < stafflines+1)
1190 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
1192 return gh_double2scm (-d * discrete_dist);
1197 Beam::has_interface (Grob*me)
1199 return me->has_interface (ly_symbol2scm ("beam-interface"));