]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam.cc
release: 1.3.109
[lilypond.git] / lily / beam.cc
1 /*
2   beam.cc -- implement Beam
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8
9 */
10
11 /*
12   [TODO]
13   * shorter! (now +- 1000 lines)
14     * less hairy code
15     * move paper vars to scm
16
17   remove *-hs variables, and do all y-position stuff in staff-space.
18 */
19
20
21 #include <math.h> // tanh.
22
23 #include "molecule.hh" 
24 #include "directional-element-interface.hh"
25 #include "beaming.hh"
26 #include "beam.hh"
27 #include "misc.hh"
28 #include "least-squares.hh"
29 #include "stem.hh"
30 #include "paper-def.hh"
31 #include "lookup.hh"
32 #include "group-interface.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "cross-staff.hh"
35 #include "item.hh"
36 #include "spanner.hh"
37 #include "warn.hh"
38
39 void
40 Beam::add_stem (Grob*me, Grob*s)
41 {
42   Pointer_group_interface:: add_element(me, "stems", s);
43   
44   s->add_dependency (me);
45
46   assert (!Stem::beam_l (s));
47   s->set_grob_property ("beam", me->self_scm ());
48
49   add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (s));
50 }
51
52 int
53 Beam::get_multiplicity (Grob*me) 
54 {
55   int m = 0;
56   for (SCM s = me->get_grob_property ("stems"); gh_pair_p (s); s = gh_cdr (s))
57     {
58       Grob * sc = unsmob_element (gh_car (s));
59
60       if (Stem::has_interface (sc))
61         m = m >? Stem::beam_count (sc,LEFT) >? Stem::beam_count (sc,RIGHT);
62     }
63   return m;
64 }
65
66 /*
67   After pre-processing all directions should be set.
68   Several post-processing routines (stem, slur, script) need stem/beam
69   direction.
70   Currenly, this means that beam has set all stem's directions.
71   [Alternatively, stems could set its own directions, according to
72    their beam, during 'final-pre-processing'.]
73  */
74 MAKE_SCHEME_CALLBACK(Beam,before_line_breaking,1);
75 SCM
76 Beam::before_line_breaking (SCM smob)
77 {
78   Grob * me =  unsmob_element (smob);
79
80   // Why?
81   if (visible_stem_count (me) < 2)
82     {
83       warning (_ ("beam has less than two stems"));
84     }
85
86   if (!Directional_element_interface::get (me))
87     Directional_element_interface::set (me, get_default_dir (me));
88
89   auto_knees (me);
90   set_stem_directions (me);
91   set_stem_shorten (me);
92
93   return SCM_EOL;
94 }
95
96 Direction
97 Beam::get_default_dir (Grob*me) 
98 {
99   Drul_array<int> total;
100   total[UP]  = total[DOWN] = 0;
101   Drul_array<int> count; 
102   count[UP]  = count[DOWN] = 0;
103   Direction d = DOWN;
104
105   Link_array<Item> stems=
106         Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
107
108   for (int i=0; i <stems.size (); i++)
109     do { // HUH -- waar slaat dit op?
110       Grob *s = stems[i];
111       Direction sd = Directional_element_interface::get (s);
112       int current = sd  ? (1 + d * sd)/2
113         : Stem::get_center_distance (s, (Direction)-d);
114
115       if (current)
116         {
117           total[d] += current;
118           count[d] ++;
119         }
120
121     } while (flip(&d) != DOWN);
122   
123   SCM func = me->get_grob_property ("dir-function");
124   SCM s = gh_call2 (func,
125                     gh_cons (gh_int2scm (count[UP]),
126                              gh_int2scm (count[DOWN])),
127                     gh_cons (gh_int2scm (total[UP]),
128                              gh_int2scm (total[DOWN])));
129
130   if (gh_number_p (s) && gh_scm2int (s))
131     return to_dir (s);
132   
133   /*
134     If dir is not determined: get default
135   */
136   return to_dir (me->get_grob_property ("default-neutral-direction"));
137 }
138
139
140 /*
141   Set all stems with non-forced direction to beam direction.
142   Urg: non-forced should become `without/with unforced' direction,
143        once stem gets cleaned-up.
144  */
145 void
146 Beam::set_stem_directions (Grob*me)
147 {
148   Link_array<Item> stems
149     =Pointer_group_interface__extract_elements (me,  (Item*) 0, "stems");
150   Direction d = Directional_element_interface::get (me);
151   
152   for (int i=0; i <stems.size (); i++)
153     {
154       Grob *s = stems[i];
155       SCM force = s->remove_grob_property ("dir-forced");
156       if (!gh_boolean_p (force) || !gh_scm2bool (force))
157         Directional_element_interface ::set (s,d);
158     }
159
160
161 void
162 Beam::auto_knees (Grob*me)
163 {
164   if (!auto_knee (me,"auto-interstaff-knee-gap", true))
165     auto_knee (me, "auto-knee-gap", false);
166 }
167
168 /*
169   Simplistic auto-knees; only consider vertical gap between two
170   adjacent chords.
171
172   `Forced' stem directions are ignored.  If you don't want auto-knees,
173   don't set, or unset autoKneeGap/autoInterstaffKneeGap.
174  */
175 bool
176 Beam::auto_knee (Grob*me, String gap_str, bool interstaff_b)
177 {
178   bool knee_b = false;
179   int knee_y = 0;
180   SCM gap = me->get_grob_property (gap_str.ch_C());
181
182   Direction d = Directional_element_interface::get (me);
183       Link_array<Item> stems=
184         Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
185   
186   if (gh_number_p (gap))
187     {
188       Spanner*sp = dynamic_cast<Spanner*> (me);
189       int auto_gap_i = gh_scm2int (gap);
190       for (int i=1; i < stems.size (); i++)
191         {
192           bool is_b = (bool)(calc_interstaff_dist (stems[i], sp) 
193             - calc_interstaff_dist (stems[i-1], sp));
194           int l_y = (int)(Stem::head_positions(stems[i-1])[d])
195             + (int)calc_interstaff_dist (stems[i-1], sp);
196           int r_y = (int)(Stem::head_positions(stems[i])[d])
197             + (int)calc_interstaff_dist (stems[i], sp);
198           int gap_i = r_y - l_y;
199
200           if ((abs (gap_i) >= auto_gap_i) && (!interstaff_b || is_b))
201             {
202               knee_y = (r_y + l_y) / 2;
203               knee_b = true;
204               break;
205             }
206         }
207     }
208   if (knee_b)
209     {
210       for (int i=0; i < stems.size (); i++)
211         {
212           Item *s = stems[i];     
213           int y = (int)(Stem::head_positions(s)[d])
214             + (int)calc_interstaff_dist (s, dynamic_cast<Spanner*> (me));
215
216           Directional_element_interface::set (s,y < knee_y ? UP : DOWN);
217           s->set_grob_property ("dir-forced", SCM_BOOL_T);
218         }
219     }
220   return knee_b;
221 }
222
223 /*
224  Set stem's shorten property if unset.
225  TODO:
226     take some y-position (chord/beam/nearest?) into account
227     scmify forced-fraction
228  */
229 void
230 Beam::set_stem_shorten (Grob*m)
231 {
232   Spanner*me = dynamic_cast<Spanner*> (m);
233   if (!visible_stem_count (me))
234     return;
235
236   Real forced_fraction = forced_stem_count (me) / visible_stem_count (me);
237   if (forced_fraction < 0.5)
238     return;
239
240   int multiplicity = get_multiplicity (me);
241
242   SCM shorten = me->get_grob_property ("beamed-stem-shorten");
243   if (shorten == SCM_EOL)
244     return;
245
246   int sz = scm_ilength (shorten);
247   
248   Real staff_space = Staff_symbol_referencer::staff_space (me);
249   SCM shorten_elt = scm_list_ref (shorten, gh_int2scm (multiplicity <? (sz - 1)));
250   Real shorten_f = gh_scm2double (shorten_elt) * staff_space;
251
252   /* cute, but who invented me -- how to customise ? */
253   if (forced_fraction < 1)
254     shorten_f /= 2;
255
256   Link_array<Item> stems=
257     Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
258
259   for (int i=0; i < stems.size (); i++)
260     {
261       Item* s = stems[i];
262       if (Stem::invisible_b (s))
263         continue;
264       if (gh_number_p (s->get_grob_property ("shorten")))
265         s->set_grob_property ("shorten", gh_double2scm (shorten_f));
266     }
267 }
268
269 /*
270   Set elt properties height and y-position if not set.
271   Adjust stem lengths to reach beam.
272  */
273 MAKE_SCHEME_CALLBACK(Beam,after_line_breaking,1);
274 SCM
275 Beam::after_line_breaking (SCM smob)
276 {
277   Grob * me =  unsmob_element (smob);
278
279   /* first, calculate y, dy */
280   Real y, dy;
281   calc_default_position_and_height (me, &y, &dy);
282   if (visible_stem_count (me))
283     {
284       if (suspect_slope_b (me, y, dy))
285         dy = 0;
286
287       Real damped_dy = calc_slope_damping_f (me, dy);
288       Real quantised_dy = quantise_dy_f (me, damped_dy);
289
290       y += (dy - quantised_dy) / 2;
291       dy = quantised_dy;
292     }
293   /*
294     until here, we used only stem_info, which acts as if dir=up
295    */
296   y *= Directional_element_interface::get (me);
297   dy *= Directional_element_interface::get (me);
298
299
300   Real half_space = Staff_symbol_referencer::staff_space (me) / 2;
301
302   /* weird: why do we do calc_position_and_height () ? regardless of
303      this setting?
304
305   */
306   /* check for user-override of dy */
307   SCM s = me->remove_grob_property ("height-hs");
308   if (gh_number_p (s))
309     {
310       dy = gh_scm2double (s) * half_space;
311     }
312   me->set_grob_property ("height", gh_double2scm (dy));
313
314   /* check for user-override of y */
315   s = me->remove_grob_property ("y-position-hs");
316   if (gh_number_p (s))
317     {
318       y = gh_scm2double (s) * half_space;
319     }
320   else
321     { 
322       /* we can modify y, so we should quantise y */
323       Real y_shift = check_stem_length_f (me, y, dy);
324       y += y_shift;
325       y = quantise_y_f (me,y, dy, 0);
326       set_stem_length (me, y, dy);
327       y_shift = check_stem_length_f (me, y, dy);
328
329       if (y_shift > half_space / 4)
330         {
331           y += y_shift;
332
333           /*
334             for significantly lengthened or shortened stems,
335             request quanting the other way.
336           */
337           int quant_dir = 0;
338           if (abs (y_shift) > half_space / 2)
339             quant_dir = sign (y_shift) * Directional_element_interface::get (me);
340           y = quantise_y_f (me, y, dy, quant_dir);
341         }
342     }
343   // UGH. Y is not in staff position unit?
344   // Ik dacht datwe daar juist van weg wilden?
345   set_stem_length (me, y, dy);
346   me->set_grob_property ("y-position", gh_double2scm (y));
347
348   return SCM_UNSPECIFIED;
349 }
350
351 /*
352   See Documentation/tex/fonts.doc
353  */
354 void
355 Beam::calc_default_position_and_height (Grob*me,Real* y, Real* dy) 
356 {
357   *y = 0;
358   *dy = 0;  
359   if (visible_stem_count (me) <= 1)
360     return;
361
362   Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
363   if (first_ideal == Stem::calc_stem_info (last_visible_stem (me)).idealy_f_)
364     {
365       *dy = 0;
366       *y = first_ideal;
367       return;
368     }
369
370   Array<Offset> ideals;
371
372   // ugh -> use commonx
373   Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
374   Link_array<Item> stems=
375     Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
376
377   for (int i=0; i < stems.size (); i++)
378     {
379       Item* s = stems[i];
380       if (Stem::invisible_b (s))
381         continue;
382       ideals.push (Offset (s->relative_coordinate (0, X_AXIS) - x0, 
383                            Stem::calc_stem_info (s).idealy_f_));
384     }
385   Real dydx;
386   minimise_least_squares (&dydx, y, ideals); // duh, takes references
387
388   Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
389   *dy = dydx * dx;
390 }
391
392 bool
393 Beam::suspect_slope_b (Grob*me, Real y, Real dy) 
394 {
395   /* first, calculate y, dy */
396   /*
397     steep slope running against lengthened stem is suspect
398   */
399   Real first_ideal = Stem::calc_stem_info (first_visible_stem (me)).idealy_f_;
400   Real last_ideal = Stem::calc_stem_info (last_visible_stem (me)).idealy_f_;
401   Real lengthened = gh_scm2double (me->get_grob_property ("outer-stem-length-limit"));
402   Real steep = gh_scm2double (me->get_grob_property ("slope-limit"));
403
404   // ugh -> use commonx
405   Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
406   Real dydx = dy && dx ? dy/dx : 0;
407
408   if (((y - first_ideal > lengthened) && (dydx > steep))
409       || ((y + dy - last_ideal > lengthened) && (dydx < -steep)))
410     {
411       return true;
412     }
413   return false;
414 }
415
416 /*
417   This neat trick is by Werner Lemberg,
418   damped = tanh (slope)
419   corresponds with some tables in [Wanske]
420 */
421 Real
422 Beam::calc_slope_damping_f (Grob*me,Real dy) 
423 {
424   SCM damp = me->get_grob_property ("damping"); 
425   int damping = gh_scm2int (damp);
426
427   if (damping)
428     {
429   // ugh -> use commonx
430       Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS)
431         - first_visible_stem (me)->relative_coordinate (0, X_AXIS);
432       Real dydx = dy && dx ? dy/dx : 0;
433       dydx = 0.6 * tanh (dydx) / damping;
434       return dydx * dx;
435     }
436   return dy;
437 }
438
439 Real
440 Beam::calc_stem_y_f (Grob*me,Item* s, Real y, Real dy) 
441 {
442   int beam_multiplicity = get_multiplicity (me);
443   int stem_multiplicity = (Stem::flag_i (s) - 2) >? 0;
444
445   SCM space_proc = me->get_grob_property ("space-function");
446   SCM space = gh_call1 (space_proc, gh_int2scm (beam_multiplicity));
447
448   Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
449   Real interbeam_f = gh_scm2double (space) ;
450
451   // ugh -> use commonx
452   Real x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
453   Real dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
454   Real stem_y = (dy && dx ? (s->relative_coordinate (0, X_AXIS) - x0) / dx * dy : 0) + y;
455
456   /* knee */
457    Direction dir  = Directional_element_interface::get (me);
458    Direction sdir = Directional_element_interface::get (s);
459    
460     /* knee */
461    if (dir!= sdir)
462       {
463        stem_y -= dir 
464         * (thick / 2 + (beam_multiplicity - 1) * interbeam_f);
465
466
467       
468       // huh, why not for first visible?
469        if (Staff_symbol_referencer::staff_symbol_l (s)
470            != Staff_symbol_referencer::staff_symbol_l (last_visible_stem (me)))
471          stem_y += Directional_element_interface::get (me)
472            * (beam_multiplicity - stem_multiplicity) * interbeam_f;
473       }
474
475   return stem_y;
476 }
477
478 Real
479 Beam::check_stem_length_f (Grob*me,Real y, Real dy) 
480 {
481   Real shorten = 0;
482   Real lengthen = 0;
483   Direction dir = Directional_element_interface::get (me);
484
485   Link_array<Item> stems=
486     Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
487
488   for (int i=0; i < stems.size(); i++)
489     {
490       Item* s = stems[i];
491       if (Stem::invisible_b (s))
492         continue;
493
494       Real stem_y = calc_stem_y_f (me, s, y, dy);
495         
496       stem_y *= dir;
497       Stem_info info = Stem::calc_stem_info (s);
498
499       // if (0 > info.maxy_f_ - stem_y)
500       shorten = shorten <? info.maxy_f_ - stem_y;
501       // if (0 < info.miny_f_ - stem_y)
502       lengthen = lengthen >? info.miny_f_ - stem_y; 
503     }
504
505   if (lengthen && shorten)
506     warning (_ ("weird beam vertical offset"));
507
508   /* when all stems are too short, normal stems win */
509   return dir * ((shorten) ?  shorten : lengthen);
510 }
511
512 /*
513   Hmm.  At this time, beam position and slope are determined.  Maybe,
514   stem directions and length should set to relative to the chord's
515   position of the beam.  */
516 void
517 Beam::set_stem_length (Grob*me,Real y, Real dy)
518 {
519   Real half_space = Staff_symbol_referencer::staff_space (me)/2;
520   Link_array<Item> stems=
521     Pointer_group_interface__extract_elements (me, (Item*)0, "stems");
522
523
524   for (int i=0; i < stems.size (); i++)
525     {
526       Item* s = stems[i];
527       if (Stem::invisible_b (s))
528         continue;
529
530       Real stem_y = calc_stem_y_f (me, s, y, dy);
531
532       /* caution: stem measures in staff-positions */
533       Stem::set_stemend (s,(stem_y + calc_interstaff_dist (s, dynamic_cast<Spanner*> (me))) / half_space);
534     }
535 }
536
537 /*
538   [Ross] (simplification of)
539   Set dy complying with:
540     - zero
541     - thick / 2 + staffline_f / 2
542     - thick + staffline_f
543   + n * staff_space
544 */
545 Real
546 Beam::quantise_dy_f (Grob*me,Real dy) 
547 {
548   Array<Real> a;
549
550   SCM proc = me->get_grob_property ("height-quants");
551   SCM quants = gh_call2 (proc, me->self_scm (),
552                          gh_double2scm (me->paper_l ()->get_var ("stafflinethickness")
553                                         / 1.0));
554   
555   
556   for (SCM s = quants; gh_pair_p (s); s = gh_cdr (s))
557     a.push (gh_scm2double (gh_car (s)));
558   
559   if (a.size () <= 1)
560     return dy;
561
562   Real staff_space = Staff_symbol_referencer::staff_space (me);
563   
564   Interval iv = quantise_iv (a, abs (dy)/staff_space) * staff_space;
565   Real q = (abs (dy) - iv[SMALLER] <= iv[BIGGER] - abs (dy))
566     ? iv[SMALLER]
567     : iv[BIGGER];
568   
569   return q * sign (dy);
570 }
571
572 /*
573   Prevent interference from stafflines and beams.
574   See Documentation/tex/fonts.doc
575
576   We only need to quantise the (left) y-position of the beam,
577   since dy is quantised too.
578   if extend_b then stems must *not* get shorter
579  */
580 Real
581 Beam::quantise_y_f (Grob*me,Real y, Real dy, int quant_dir)
582 {
583   int multiplicity = get_multiplicity (me);
584
585   Real staff_space = Staff_symbol_referencer::staff_space (me);
586   Real thick = me->paper_l ()->get_var ("stafflinethickness");
587
588
589   SCM proc = me->get_grob_property ("vertical-position-quant-function");
590   SCM quants = scm_apply (proc,
591                           me->self_scm (),
592                           gh_list (gh_int2scm (multiplicity),
593                                    gh_double2scm (dy/staff_space),
594                                    gh_double2scm (thick/staff_space),
595                                    SCM_EOL, SCM_UNDEFINED));
596   
597   Array<Real> a;
598
599   for (; gh_pair_p (quants); quants = gh_cdr (quants))
600     a.push (gh_scm2double (gh_car (quants)));
601
602   if (a.size () <= 1)
603     return y;
604
605   Real up_y = Directional_element_interface::get (me) * y;
606   Interval iv = quantise_iv (a, up_y/staff_space) * staff_space;
607
608   Real q = up_y - iv[SMALLER] <= iv[BIGGER] - up_y 
609     ? iv[SMALLER] : iv[BIGGER];
610   if (quant_dir)
611     q = iv[(Direction)quant_dir];
612
613   return q * Directional_element_interface::get (me);
614 }
615
616 void
617 Beam::set_beaming (Grob*me,Beaming_info_list *beaming)
618 {
619   Link_array<Grob> stems=
620     Pointer_group_interface__extract_elements (me, (Grob*)0, "stems");
621   
622   Direction d = LEFT;
623   for (int i=0; i  < stems.size(); i++)
624     {
625       do
626         {
627           if (Stem::beam_count (stems[i], d) == 0)
628             Stem::set_beaming ( stems[i], beaming->infos_.elem (i).beams_i_drul_[d],d);
629         }
630       while (flip (&d) != LEFT);
631     }
632 }
633
634
635
636 /*
637   beams to go with one stem.
638
639   FIXME: clean me up.
640   */
641 Molecule
642 Beam::stem_beams (Grob*me,Item *here, Item *next, Item *prev) 
643 {
644   // ugh -> use commonx
645   if ((next && !(next->relative_coordinate (0, X_AXIS) > here->relative_coordinate (0, X_AXIS))) ||
646       (prev && !(prev->relative_coordinate (0, X_AXIS) < here->relative_coordinate (0, X_AXIS))))
647       programming_error ("Beams are not left-to-right");
648
649   Real staffline_f = me->paper_l ()->get_var ("stafflinethickness");
650   int multiplicity = get_multiplicity (me);
651
652   SCM space_proc = me->get_grob_property ("space-function");
653   SCM space = gh_call1 (space_proc, gh_int2scm (multiplicity));
654
655   Real thick = gh_scm2double (me->get_grob_property ("thickness")) ;
656   Real interbeam_f = gh_scm2double (space) ;
657     
658   Real bdy = interbeam_f;
659   Real stemdx = staffline_f;
660
661     // ugh -> use commonx
662   Real dx = visible_stem_count (me) ?
663     last_visible_stem (me)->relative_coordinate (0, X_AXIS) - first_visible_stem (me)->relative_coordinate (0, X_AXIS)
664     : 0.0;
665   Real dy = gh_scm2double (me->get_grob_property ("height"));
666   Real dydx = dy && dx ? dy/dx : 0;
667
668   Molecule leftbeams;
669   Molecule rightbeams;
670
671   Real nw_f;
672   if (!Stem::first_head (here))
673     nw_f = 0;
674   else {
675     int t = Stem::type_i (here); 
676
677     SCM proc = me->get_grob_property ("flag-width-function");
678     SCM result = gh_call1 (proc, gh_int2scm (t));
679     nw_f = gh_scm2double (result);
680   }
681
682
683   Direction dir = Directional_element_interface::get (me);
684   
685   /* half beams extending to the left. */
686   if (prev)
687     {
688       int lhalfs= lhalfs = Stem::beam_count (here,LEFT) - Stem::beam_count (prev,RIGHT);
689       int lwholebeams= Stem::beam_count (here,LEFT) <? Stem::beam_count (prev,RIGHT) ;
690       /*
691        Half beam should be one note-width, 
692        but let's make sure two half-beams never touch
693        */
694       Real w = here->relative_coordinate (0, X_AXIS) - prev->relative_coordinate (0, X_AXIS);
695       w = w/2 <? nw_f;
696       Molecule a;
697       if (lhalfs)               // generates warnings if not
698         a =  Lookup::beam (dydx, w, thick);
699       a.translate (Offset (-w, -w * dydx));
700       for (int j = 0; j  < lhalfs; j++)
701         {
702           Molecule b (a);
703           b.translate_axis (-dir * bdy * (lwholebeams+j), Y_AXIS);
704           leftbeams.add_molecule (b);
705         }
706     }
707
708   if (next)
709     {
710       int rhalfs  = Stem::beam_count (here,RIGHT) - Stem::beam_count (next,LEFT);
711       int rwholebeams= Stem::beam_count (here,RIGHT) <? Stem::beam_count (next,LEFT) ;
712
713       Real w = next->relative_coordinate (0, X_AXIS) - here->relative_coordinate (0, X_AXIS);
714       Molecule a = Lookup::beam (dydx, w + stemdx, thick);
715       a.translate_axis( - stemdx/2, X_AXIS);
716       int j = 0;
717       Real gap_f = 0;
718
719       SCM gap = me->get_grob_property ("gap");
720       if (gh_number_p (gap))
721         {
722           int gap_i = gh_scm2int ( (gap));
723           int nogap = rwholebeams - gap_i;
724           
725           for (; j  < nogap; j++)
726             {
727               Molecule b (a);
728               b.translate_axis (-dir  * bdy * j, Y_AXIS);
729               rightbeams.add_molecule (b);
730             }
731           // TODO: notehead widths differ for different types
732           gap_f = nw_f / 2;
733           w -= 2 * gap_f;
734           a = Lookup::beam (dydx, w + stemdx, thick);
735         }
736
737       for (; j  < rwholebeams; j++)
738         {
739           Molecule b (a);
740           b.translate (Offset (Stem::invisible_b (here) ? 0 : gap_f, -dir * bdy * j));
741           rightbeams.add_molecule (b);
742         }
743
744       w = w/2 <? nw_f;
745       if (rhalfs)
746         a = Lookup::beam (dydx, w, thick);
747
748       for (; j  < rwholebeams + rhalfs; j++)
749         {
750           Molecule b (a);
751           b.translate_axis (- dir * bdy * j, Y_AXIS);
752           rightbeams.add_molecule (b);
753         }
754
755     }
756   leftbeams.add_molecule (rightbeams);
757
758   /*
759     Does beam quanting think  of the asymetry of beams? 
760     Refpoint is on bottom of symbol. (FIXTHAT) --hwn.
761    */
762   return leftbeams;
763 }
764
765 MAKE_SCHEME_CALLBACK(Beam,brew_molecule,1);
766 SCM
767 Beam::brew_molecule (SCM smob)
768 {
769   Grob * me =unsmob_element (smob);
770
771   Molecule mol;
772   if (!gh_pair_p (me->get_grob_property ("stems")))
773     return SCM_EOL;
774   Real x0,dx;
775   Link_array<Item>stems = 
776     Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");  
777   if (visible_stem_count (me))
778     {
779   // ugh -> use commonx
780       x0 = first_visible_stem (me)->relative_coordinate (0, X_AXIS);
781       dx = last_visible_stem (me)->relative_coordinate (0, X_AXIS) - x0;
782     }
783   else
784     {
785       x0 = stems[0]->relative_coordinate (0, X_AXIS);
786       dx = stems.top()->relative_coordinate (0, X_AXIS) - x0;
787     }
788   
789   
790   Real dy = gh_scm2double (me->get_grob_property ("height"));
791   Real dydx = dy && dx ? dy/dx : 0;
792   Real y = gh_scm2double (me->get_grob_property ("y-position"));
793
794
795   for (int j=0; j <stems.size  (); j++)
796     {
797       Item *i = stems[j];
798       Item * prev = (j > 0)? stems[j-1] : 0;
799       Item * next = (j < stems.size()-1) ? stems[j+1] :0;
800
801       Molecule sb = stem_beams (me, i, next, prev);
802       Real x = i->relative_coordinate (0, X_AXIS)-x0;
803       sb.translate (Offset (x, x * dydx + y));
804       mol.add_molecule (sb);
805     }
806   mol.translate_axis (x0 
807     - dynamic_cast<Spanner*> (me)->get_bound (LEFT)->relative_coordinate (0, X_AXIS), X_AXIS);
808
809   return mol.smobbed_copy ();
810 }
811
812 int
813 Beam::forced_stem_count (Grob*me) 
814 {
815   Link_array<Item>stems = 
816     Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
817   int f = 0;
818   for (int i=0; i < stems.size (); i++)
819     {
820       Item *s = stems[i];
821
822       if (Stem::invisible_b (s))
823         continue;
824
825       if (((int)Stem::chord_start_f (s)) 
826         && (Stem::get_direction (s ) != Stem::get_default_dir (s )))
827         f++;
828     }
829   return f;
830 }
831
832
833
834
835 /* TODO:
836    use filter and standard list functions.
837  */
838 int
839 Beam::visible_stem_count (Grob*me) 
840 {
841   Link_array<Item>stems = 
842     Pointer_group_interface__extract_elements (me, (Item*) 0, "stems");
843   int c = 0;
844   for (int i = stems.size (); i--;)
845     {
846       if (!Stem::invisible_b (stems[i]))
847         c++;
848     }
849   return c;
850 }
851
852 Item*
853 Beam::first_visible_stem(Grob*me) 
854 {
855   Link_array<Item>stems = 
856     Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
857   
858   for (int i = 0; i < stems.size (); i++)
859     {
860       if (!Stem::invisible_b (stems[i]))
861         return stems[i];
862     }
863   return 0;
864 }
865
866 Item*
867 Beam::last_visible_stem(Grob*me) 
868 {
869   Link_array<Item>stems = 
870     Pointer_group_interface__extract_elements ( me, (Item*) 0, "stems");
871   for (int i = stems.size (); i--;)
872     {
873       if (!Stem::invisible_b (stems[i]))
874         return stems[i];
875     }
876   return 0;
877 }
878
879
880 /*
881   [TODO]
882   handle rest under beam (do_post: beams are calculated now)
883   what about combination of collisions and rest under beam.
884
885   Should lookup
886     
887     rest -> stem -> beam -> interpolate_y_position ()
888 */
889 MAKE_SCHEME_CALLBACK(Beam,rest_collision_callback,2);
890 SCM
891 Beam::rest_collision_callback (SCM element_smob, SCM axis)
892 {
893   Grob *rest = unsmob_element (element_smob);
894   Axis a = (Axis) gh_scm2int (axis);
895   
896   assert (a == Y_AXIS);
897
898   Grob * st = unsmob_element (rest->get_grob_property ("stem"));
899   Grob * stem = st;
900   if (!stem)
901     return gh_double2scm (0.0);
902   Grob * beam = unsmob_element (stem->get_grob_property ("beam"));
903   if (!beam || !Beam::has_interface (beam) || !Beam::visible_stem_count (beam))
904     return gh_double2scm (0.0);
905
906   // make callback for rest from this.
907   Real beam_dy = 0;
908   Real beam_y = 0;
909
910
911   // todo: make sure this calced already.
912   SCM s = beam->get_grob_property ("height");
913   if (gh_number_p (s))
914     beam_dy = gh_scm2double (s);
915   
916   s = beam->get_grob_property ("y-position");
917   if (gh_number_p (s))
918     beam_y = gh_scm2double (s);
919   
920   // ugh -> use commonx
921   Real x0 = first_visible_stem(beam)->relative_coordinate (0, X_AXIS);
922   Real dx = last_visible_stem(beam)->relative_coordinate (0, X_AXIS) - x0;
923   Real dydx = beam_dy && dx ? beam_dy/dx : 0;
924
925   Direction d = Stem::get_direction (stem);
926   Real beamy = (stem->relative_coordinate (0, X_AXIS) - x0) * dydx + beam_y;
927
928   Real staff_space =   Staff_symbol_referencer::staff_space (rest);
929
930   
931   Real rest_dim = rest->extent (rest, Y_AXIS)[d]*2.0 / staff_space ; // refp??
932
933   Real minimum_dist
934     = gh_scm2double (rest->get_grob_property ("minimum-beam-collision-distance"));
935   Real dist =
936     minimum_dist +  -d  * (beamy - rest_dim) >? 0;
937
938   int stafflines = Staff_symbol_referencer::line_count (rest);
939
940   // move discretely by half spaces.
941   int discrete_dist = int (ceil (dist));
942
943   // move by whole spaces inside the staff.
944   if (discrete_dist < stafflines+1)
945     discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
946
947   return gh_double2scm  (-d *  discrete_dist);
948 }
949
950
951 bool
952 Beam::has_interface (Grob*me)
953 {
954   return me->has_interface (ly_symbol2scm ("beam-interface"));
955 }
956
957 void
958 Beam::set_interface (Grob*me)
959 {
960   /*
961     why the init? No way to tell difference between default and user
962     override.  */
963   me->set_grob_property ("height", gh_int2scm (0)); // ugh.
964   me->set_grob_property ("y-position" ,gh_int2scm (0));
965   me->set_interface (ly_symbol2scm("beam-interface"));
966 }