]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam.cc
5cc6c7c84587e436cdfae706bf7ca92c103ac722
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 /*
11   TODO:
12
13   - Determine auto knees based on positions if it's set by the user.
14
15   - the code is littered with * and / staff_space calls for
16   #'positions. Consider moving to real-world coordinates?
17
18   Problematic issue is user tweaks (user tweaks are in staff-coordinates.)
19
20   Notes:
21
22   - Stems run to the Y-center of the beam.
23
24   - beam_translation is the offset between Y centers of the beam.
25 */
26
27 #include "beam.hh"
28
29 #include "beaming-pattern.hh"
30 #include "directional-element-interface.hh"
31 #include "main.hh"
32 #include "international.hh"
33 #include "interval-set.hh"
34 #include "item.hh"
35 #include "least-squares.hh"
36 #include "lookup.hh"
37 #include "misc.hh"
38 #include "output-def.hh"
39 #include "pointer-group-interface.hh"
40 #include "spanner.hh"
41 #include "staff-symbol-referencer.hh"
42 #include "stem.hh"
43 #include "warn.hh"
44 #include "grob-array.hh"
45
46 #if DEBUG_BEAM_SCORING
47 #include "text-interface.hh" // debug output.
48 #include "font-interface.hh" // debug output.
49 #endif
50
51 #include <map>
52
53
54 Beam_stem_segment::Beam_stem_segment ()
55 {
56   max_connect_ = 1000;          // infinity
57   stem_ = 0;
58   width_ = 0.0;
59   stem_x_ = 0.0;
60   rank_ = 0;
61   stem_index_ = 0;
62   dir_ = CENTER;
63 }
64
65 Beam_segment::Beam_segment ()
66 {
67   vertical_count_ = 0;
68 }
69
70 void
71 Beam::add_stem (Grob *me, Grob *s)
72 {
73   if (Stem::get_beam (s))
74     {
75       programming_error ("Stem already has beam");
76       return ;
77     }
78
79   Pointer_group_interface::add_grob (me, ly_symbol2scm ("stems"), s);
80   s->set_object ("beam", me->self_scm ());
81   add_bound_item (dynamic_cast<Spanner *> (me), dynamic_cast<Item *> (s));
82 }
83
84 Real
85 Beam::get_thickness (Grob *me)
86 {
87   return robust_scm2double (me->get_property ("thickness"), 0)
88     * Staff_symbol_referencer::staff_space (me);
89 }
90
91 /* Return the translation between 2 adjoining beams. */
92 Real
93 Beam::get_beam_translation (Grob *me)
94 {
95   int beam_count = get_beam_count (me);
96   Real staff_space = Staff_symbol_referencer::staff_space (me);
97   Real line = Staff_symbol_referencer::line_thickness (me);
98   Real thickness = get_thickness (me);
99   Real fract = robust_scm2double (me->get_property ("length-fraction"), 1.0);
100   
101   Real beam_translation = beam_count < 4
102     ? (2 * staff_space + line - thickness) / 2.0
103     : (3 * staff_space + line - thickness) / 3.0;
104
105   return fract * beam_translation;
106 }
107
108 /* Maximum beam_count. */
109 int
110 Beam::get_beam_count (Grob *me)
111 {
112   int m = 0;
113
114   extract_grob_set (me, "stems", stems);
115   for (vsize i = 0; i < stems.size (); i++)
116     {
117       Grob *stem = stems[i];
118       m = max (m, (Stem::beam_multiplicity (stem).length () + 1));
119     }
120   return m;
121 }
122
123 MAKE_SCHEME_CALLBACK (Beam, calc_normal_stems, 1);
124 SCM
125 Beam::calc_normal_stems (SCM smob)
126 {
127   Grob *me = unsmob_grob (smob);
128   
129   extract_grob_set (me, "stems", stems);
130   SCM val = Grob_array::make_array ();
131   Grob_array *ga = unsmob_grob_array (val);
132   for (vsize i = 0; i < stems.size ();  i++)
133     if (Stem::is_normal_stem (stems[i]))
134       ga->add (stems[i]);
135   
136   return val;  
137 }
138
139 MAKE_SCHEME_CALLBACK (Beam, calc_direction, 1);
140 SCM
141 Beam::calc_direction (SCM smob)
142 {
143   Grob *me = unsmob_grob (smob);
144
145   /* Beams with less than 2 two stems don't make much sense, but could happen
146      when you do
147
148      r8[ c8 r8]
149
150   */
151
152   Direction dir = CENTER;
153
154   int count = normal_stem_count (me);
155   if (count < 2)
156     {
157       extract_grob_set (me, "stems", stems);
158       if (stems.size () == 0)
159         {
160           me->warning (_ ("removing beam with no stems"));
161           me->suicide ();
162
163           return SCM_UNSPECIFIED;
164         }
165       else 
166         {
167           Grob *stem = first_normal_stem (me);
168
169           /*
170             This happens for chord tremolos.
171           */
172           if (!stem)
173             stem = stems[0];
174           
175           if (is_direction (stem->get_property_data ("direction"))) 
176             dir = to_dir (stem->get_property_data ("direction"));
177           else
178             dir = to_dir (stem->get_property ("default-direction"));
179         }
180     }
181
182   if (count >= 1)
183     {
184       if (!dir)
185         dir = get_default_dir (me);
186       
187       consider_auto_knees (me);
188     }
189
190   if (dir)
191     {
192       set_stem_directions (me, dir);
193     }
194   
195   return scm_from_int (dir);
196 }
197
198
199
200 /* We want a maximal number of shared beams, but if there is choice, we
201  * take the one that is closest to the end of the stem. This is for
202  * situations like
203  *
204  *        x
205  *       |
206  *       |
207  *   |===|
208  *   |=
209  *   |
210  *  x
211  */
212 int
213 position_with_maximal_common_beams (SCM left_beaming, SCM right_beaming,
214                                     Direction left_dir,
215                                     Direction right_dir)
216 {
217   Slice lslice = int_list_to_slice (scm_cdr (left_beaming));
218
219   int best_count = 0;
220   int best_start = 0;
221   for (int i = lslice[-left_dir];
222        (i - lslice[left_dir]) * left_dir <= 0; i += left_dir)
223     {
224       int count = 0;
225       for (SCM s = scm_car (right_beaming); scm_is_pair (s); s = scm_cdr (s))
226         {
227           int k = -right_dir * scm_to_int (scm_car (s)) + i;
228           if (scm_c_memq (scm_from_int (k), left_beaming) != SCM_BOOL_F)
229             count++;
230         }
231
232       if (count >= best_count)
233         {
234           best_count = count;
235           best_start = i;
236         }
237     }
238
239   return best_start;
240 }
241
242 MAKE_SCHEME_CALLBACK (Beam, calc_beaming, 1)
243 SCM
244 Beam::calc_beaming (SCM smob)
245 {
246   Grob *me = unsmob_grob (smob);
247   
248   extract_grob_set (me, "stems", stems);
249
250   Slice last_int;
251   last_int.set_empty ();
252   
253   SCM last_beaming = scm_cons (SCM_EOL, scm_list_1 (scm_from_int (0)));
254   Direction last_dir = CENTER;
255   for (vsize i = 0; i < stems.size (); i++)
256     {
257       Grob *this_stem = stems[i];
258       SCM this_beaming = this_stem->get_property ("beaming");
259
260       Direction this_dir = get_grob_direction (this_stem);
261       if (scm_is_pair (last_beaming) && scm_is_pair (this_beaming))
262         {
263           int start_point = position_with_maximal_common_beams
264             (last_beaming, this_beaming,
265              last_dir ? last_dir : this_dir,
266              this_dir);
267
268           Direction d = LEFT;
269           Slice new_slice;
270           do
271             {
272               new_slice.set_empty ();
273               SCM s = index_get_cell (this_beaming, d);
274               for (; scm_is_pair (s); s = scm_cdr (s))
275                 {
276                   int new_beam_pos
277                     = start_point - this_dir * scm_to_int (scm_car (s));
278
279                   new_slice.add_point (new_beam_pos);
280                   scm_set_car_x (s, scm_from_int (new_beam_pos));
281                 }
282             }
283           while (flip (&d) != LEFT);
284
285           if (!new_slice.is_empty ())
286             last_int = new_slice;
287         }
288       else
289         {
290           /*
291             FIXME: what's this for? 
292            */
293           SCM s = scm_cdr (this_beaming);
294           for (; scm_is_pair (s); s = scm_cdr (s))
295             {
296               int np = -this_dir * scm_to_int (scm_car (s));
297               scm_set_car_x (s, scm_from_int (np));
298               last_int.add_point (np);
299             }
300         }
301       
302       if (scm_ilength (scm_cdr (this_beaming)) > 0)
303         {
304           last_beaming = this_beaming;
305           last_dir = this_dir;
306         }
307     }
308
309   return SCM_EOL;
310 }
311
312 bool
313 operator <(Beam_stem_segment const &a,
314            Beam_stem_segment const &b)
315 {
316   return a.rank_ < b.rank_;
317 }
318
319 typedef map<int, vector<Beam_stem_segment> >  Position_stem_segments_map; 
320
321 vector<Beam_segment>
322 Beam::get_beam_segments (Grob *me_grob, Grob **common)
323 {
324   /* ugh, this has a side-effect that we need to ensure that
325      Stem #'beaming is correct */
326   (void) me_grob->get_property ("beaming");
327
328   Spanner *me = dynamic_cast<Spanner*> (me_grob);
329
330   extract_grob_set (me, "stems", stems);
331   Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
332
333   commonx = me->get_bound (LEFT)->common_refpoint (commonx, X_AXIS);
334   commonx = me->get_bound (RIGHT)->common_refpoint (commonx, X_AXIS);
335
336   *common = commonx;
337   
338   int gap_count = robust_scm2int (me->get_property ("gap-count"), 0);
339   Real gap_length = robust_scm2double (me->get_property ("gap"), 0.0);
340
341   Position_stem_segments_map stem_segments;
342   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
343
344   Slice ranks;
345   for (vsize i = 0; i < stems.size (); i++)
346     {
347       Grob *stem = stems[i];
348       Real stem_width = robust_scm2double (stem->get_property ("thickness"), 1.0) * lt;
349       Real stem_x = stem->relative_coordinate (commonx, X_AXIS);
350       SCM beaming = stem->get_property ("beaming");
351       Direction d = LEFT;
352       do
353         {
354           for (SCM s = index_get_cell (beaming, d);
355                scm_is_pair (s); s = scm_cdr (s))
356             {
357               if (!scm_is_integer (scm_car (s)))
358                 continue;
359
360               int beam_rank = scm_to_int (scm_car (s));
361               ranks.add_point (beam_rank);
362             }
363           
364           for (SCM s = index_get_cell (beaming, d);
365                scm_is_pair (s); s = scm_cdr (s))
366             {
367               if (!scm_is_integer (scm_car (s)))
368                 continue;
369           
370               int beam_rank = scm_to_int (scm_car (s));
371               Beam_stem_segment seg;
372               seg.stem_ = stem;
373               seg.stem_x_ = stem_x;
374               seg.rank_ = 2 * i + (d+1)/2;
375               seg.width_ = stem_width;
376               seg.stem_index_ = i;
377               seg.dir_ = d;
378               seg.max_connect_ = robust_scm2int (stem->get_property ("max-beam-connect"), 1000);
379               
380               Direction stem_dir = get_grob_direction (stem);
381               
382               seg.gapped_
383                 = (stem_dir * beam_rank < (stem_dir * ranks[-stem_dir] + gap_count));
384               stem_segments[beam_rank].push_back (seg);
385             }
386         }
387       while (flip (&d) != LEFT);
388     }
389
390   Drul_array<Real> break_overshoot
391     = robust_scm2drul (me->get_property ("break-overshoot"),
392                        Drul_array<Real> (-0.5, 0.0));
393
394   vector<Beam_segment> segments;
395   for (Position_stem_segments_map::const_iterator i (stem_segments.begin ());
396        i != stem_segments.end (); i++)
397     {
398       vector<Beam_stem_segment> segs = (*i).second;
399       vector_sort (segs, less<Beam_stem_segment> ());
400
401       Beam_segment current;
402
403       int vertical_count =  (*i).first;
404       for (vsize j = 0; j < segs.size (); j++)
405         {
406           /*
407             event_dir == LEFT: left edge of a beamsegment.
408            */
409           Direction event_dir = LEFT;
410           do
411             {
412               bool on_line_bound = (segs[j].dir_ == LEFT) ? segs[j].stem_index_ == 0
413                 : segs[j].stem_index_ == stems.size() - 1;
414               bool on_beam_bound = (event_dir == LEFT) ? j == 0 :
415                 j == segs.size () - 1;
416               bool inside_stem = (event_dir == LEFT)
417                 ? segs[j].stem_index_ > 0
418                 : segs[j].stem_index_ + 1 < stems.size () ;
419                       
420               bool event = on_beam_bound
421                 || abs (segs[j].rank_ - segs[j+event_dir].rank_) > 1
422                 || (abs (vertical_count) >= segs[j].max_connect_
423                     || abs (vertical_count) >= segs[j + event_dir].max_connect_);
424               
425               if (!event)
426                 continue;
427
428               current.vertical_count_ = vertical_count;
429               current.horizontal_[event_dir] = segs[j].stem_x_;
430               if (segs[j].dir_ == event_dir)
431                 {
432                   if (on_line_bound
433                       && me->get_bound (event_dir)->break_status_dir ())
434                     {
435                       current.horizontal_[event_dir]
436                         = (robust_relative_extent (me->get_bound (event_dir),
437                                                    commonx, X_AXIS)[RIGHT]
438                            + event_dir * break_overshoot[event_dir]);
439                     }
440                   else
441                     {
442                       Real notehead_width = 
443                         Stem::duration_log (segs[j].stem_) == 1
444                         ? 1.98
445                         : 1.32; // URG.
446
447
448                       if (inside_stem)
449                         {
450                           Grob *neighbor_stem = stems[segs[j].stem_index_ + event_dir];
451                           Real neighbor_stem_x
452                             = neighbor_stem->relative_coordinate (commonx, X_AXIS);
453
454                           notehead_width = min (notehead_width,
455                                                 fabs (neighbor_stem_x - segs[j].stem_x_)/2.5);
456                         }
457                       current.horizontal_[event_dir] += event_dir * notehead_width;
458                     }
459                 }
460               else
461                 {
462                   current.horizontal_[event_dir] += event_dir * segs[j].width_/2;
463                   if (segs[j].gapped_)
464                     {
465                       current.horizontal_[event_dir] -= event_dir * gap_length;
466
467                       if (Stem::is_invisible (segs[j].stem_))
468                         {
469                           /*
470                             Need to do this in case of whole notes. We don't want the
471                             heads to collide with the beams.
472                            */
473                           extract_grob_set (segs[j].stem_, "note-heads", heads);
474
475                           for (vsize k = 0; k < heads.size (); k ++)
476                             current.horizontal_[event_dir]
477                               = event_dir * min  (event_dir * current.horizontal_[event_dir],
478                                                   - gap_length/2
479                                                   + event_dir
480                                                     * heads[k]->extent (commonx,
481                                                                         X_AXIS)[-event_dir]);
482                         }
483                     }
484                 }
485
486               if (event_dir == RIGHT)
487                 {
488                   segments.push_back (current);
489                   current = Beam_segment ();
490                 }
491             }
492           while (flip (&event_dir) != LEFT);
493         }
494       
495     }
496
497   return segments;
498 }
499
500 MAKE_SCHEME_CALLBACK (Beam, print, 1);
501 SCM
502 Beam::print (SCM grob)
503 {
504   Spanner *me = unsmob_spanner (grob);
505   Grob *commonx = 0;
506   vector<Beam_segment> segments = get_beam_segments (me, &commonx);
507
508   Interval span;
509   if (normal_stem_count (me))
510     {
511       span[LEFT] = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
512       span[RIGHT] = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
513     }
514   else
515     {
516       extract_grob_set (me, "stems", stems);      
517       span[LEFT] = stems[0]->relative_coordinate (commonx, X_AXIS);
518       span[RIGHT] = stems.back ()->relative_coordinate (commonx, X_AXIS);
519     }
520
521   Real blot = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
522
523   SCM posns = me->get_property ("quantized-positions");
524   Interval pos;
525   if (!is_number_pair (posns))
526     {
527       programming_error ("no beam positions?");
528       pos = Interval (0, 0);
529     }
530   else
531     pos = ly_scm2realdrul (posns);
532
533   scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
534
535   Real dy = pos[RIGHT] - pos[LEFT];
536   Real slope = (dy && span.length ()) ? dy / span.length ()  : 0;
537
538   Real thick = get_thickness (me);
539   Real beam_dy = get_beam_translation (me);
540
541   Direction feather_dir = to_dir (me->get_property ("grow-direction"));
542   
543   Stencil the_beam;
544   for (vsize i = 0; i < segments.size (); i ++)
545     {
546       Real local_slope = slope;
547       if (feather_dir)
548         {
549           local_slope += feather_dir * segments[i].vertical_count_ * beam_dy / span.length ();
550         }
551       
552       Stencil b = Lookup::beam (local_slope, segments[i].horizontal_.length (), thick, blot);
553
554       b.translate_axis (segments[i].horizontal_[LEFT], X_AXIS);
555       
556       b.translate_axis (local_slope
557                         * (segments[i].horizontal_[LEFT] - span.linear_combination (feather_dir))
558                         + pos.linear_combination (feather_dir)
559                         + beam_dy * segments[i].vertical_count_, Y_AXIS);
560       the_beam.add_stencil (b);      
561     }
562          
563 #if (DEBUG_BEAM_SCORING)
564   SCM annotation = me->get_property ("annotation");
565   if (!scm_is_string (annotation))
566     {
567       SCM debug = me->layout ()->lookup_variable (ly_symbol2scm ("debug-beam-scoring"));
568       if (to_boolean (debug))
569         annotation = me->get_property ("quant-score");
570     }
571   
572   if (scm_is_string (annotation))
573     {
574       extract_grob_set (me, "stems", stems);      
575
576       /*
577         This code prints the demerits for each beam. Perhaps this
578         should be switchable for those who want to twiddle with the
579         parameters.
580       */
581       string str;
582       SCM properties = Font_interface::text_font_alist_chain (me);
583
584       Direction stem_dir = stems.size () ? to_dir (stems[0]->get_property ("direction")) : UP;
585
586       Stencil score = *unsmob_stencil (Text_interface::interpret_markup
587                                     (me->layout ()->self_scm (), properties, annotation));
588
589       if (!score.is_empty ())
590         {
591           score.translate_axis (me->relative_coordinate(commonx, X_AXIS), X_AXIS);
592           the_beam.add_at_edge (Y_AXIS, stem_dir, score, 1.0);
593         }
594     }
595 #endif
596
597   the_beam.translate_axis (-me->relative_coordinate (commonx, X_AXIS), X_AXIS);
598   return the_beam.smobbed_copy ();
599 }
600  
601 Direction
602 Beam::get_default_dir (Grob *me)
603 {
604   extract_grob_set (me, "stems", stems);
605
606   Drul_array<Real> extremes (0.0, 0.0);
607   for (iterof (s, stems); s != stems.end (); s++)
608     {
609       Interval positions = Stem::head_positions (*s);
610       Direction d = DOWN;
611       do
612         {
613           if (sign (positions[d]) == d)
614             extremes[d] = d * max (d * positions[d], d * extremes[d]);
615         }
616       while (flip (&d) != DOWN);
617     }
618
619   Drul_array<int> total (0, 0);
620   Drul_array<int> count (0, 0);
621
622   bool force_dir = false;
623   for (vsize i = 0; i < stems.size (); i++)
624     {
625       Grob *s = stems[i];
626       Direction stem_dir = CENTER;
627       SCM stem_dir_scm = s->get_property_data ("direction");
628       if (is_direction (stem_dir_scm))
629         {
630           stem_dir = to_dir (stem_dir_scm);
631           force_dir = true;
632         }
633       else
634         stem_dir = to_dir (s->get_property ("default-direction"));
635
636       if (!stem_dir)
637         stem_dir = to_dir (s->get_property ("neutral-direction"));
638
639       if (stem_dir)
640         {
641           count[stem_dir] ++;
642           total[stem_dir] += max (int (- stem_dir * Stem::head_positions (s) [-stem_dir]), 0);
643         }
644     }
645
646
647   if (!force_dir)
648     {
649       if (abs (extremes[UP]) > -extremes[DOWN])
650         return DOWN;
651       else if (extremes[UP] < -extremes[DOWN])
652         return UP;
653     }
654   
655   Direction dir = CENTER;
656   Direction d = CENTER;
657   if ((d = (Direction) sign (count[UP] - count[DOWN])))
658     dir = d;
659   else if (count[UP]
660            && count[DOWN]
661            && (d = (Direction)  sign (total[UP] / count[UP] - total[DOWN]/count[DOWN])))
662     dir = d;
663   else if ((d = (Direction)  sign (total[UP] - total[DOWN])))
664     dir = d;
665   else
666     dir = to_dir (me->get_property ("neutral-direction"));
667   
668   return dir;
669 }
670
671 /* Set all stems with non-forced direction to beam direction.
672    Urg: non-forced should become `without/with unforced' direction,
673    once stem gets cleaned-up. */
674 void
675 Beam::set_stem_directions (Grob *me, Direction d)
676 {
677   extract_grob_set (me, "stems", stems);
678
679   for (vsize i = 0; i < stems.size (); i++)
680     {
681       Grob *s = stems[i];
682
683       SCM forcedir = s->get_property_data ("direction");
684       if (!to_dir (forcedir))
685         set_grob_direction (s, d);
686     }
687 }
688
689 /*
690   Only try horizontal beams for knees.  No reliable detection of
691   anything else is possible here, since we don't know funky-beaming
692   settings, or X-distances (slopes!)  People that want sloped
693   knee-beams, should set the directions manually.
694
695
696   TODO:
697
698   this routine should take into account the stemlength scoring
699   of a possible knee/nonknee beam.
700 */
701 void
702 Beam::consider_auto_knees (Grob *me)
703 {
704   SCM scm = me->get_property ("auto-knee-gap");
705   if (!scm_is_number (scm))
706     return;
707
708   Interval_set gaps;
709
710   gaps.set_full ();
711
712   extract_grob_set (me, "normal-stems", stems);
713
714   Grob *common = common_refpoint_of_array (stems, me, Y_AXIS);
715   Real staff_space = Staff_symbol_referencer::staff_space (me);
716
717   vector<Interval> head_extents_array;
718   for (vsize i = 0; i < stems.size (); i++)
719     {
720       Grob *stem = stems[i];
721
722       Interval head_extents = Stem::head_positions (stem);
723       if (!head_extents.is_empty ())
724         {
725           head_extents[LEFT] += -1;
726           head_extents[RIGHT] += 1;
727           head_extents *= staff_space * 0.5;
728
729           /*
730             We could subtract beam Y position, but this routine only
731             sets stem directions, a constant shift does not have an
732             influence.
733           */
734           head_extents += stem->pure_relative_y_coordinate (common, 0, INT_MAX);
735
736           if (to_dir (stem->get_property_data ("direction")))
737             {
738               Direction stemdir = to_dir (stem->get_property ("direction"));
739               head_extents[-stemdir] = -stemdir * infinity_f;
740             }
741         }
742       head_extents_array.push_back (head_extents);
743
744       gaps.remove_interval (head_extents);
745     }
746
747   Interval max_gap;
748   Real max_gap_len = 0.0;
749
750   for (vsize i = gaps.allowed_regions_.size () -1; i != VPOS ;i--)
751     {
752       Interval gap = gaps.allowed_regions_[i];
753
754       /*
755         the outer gaps are not knees.
756       */
757       if (isinf (gap[LEFT]) || isinf (gap[RIGHT]))
758         continue;
759
760       if (gap.length () >= max_gap_len)
761         {
762           max_gap_len = gap.length ();
763           max_gap = gap;
764         }
765     }
766
767   Real beam_translation = get_beam_translation (me);
768   Real beam_thickness = Beam::get_thickness (me);
769   int beam_count = Beam::get_beam_count (me);
770   Real height_of_beams = beam_thickness / 2
771     + (beam_count - 1) * beam_translation;
772   Real threshold = scm_to_double (scm) + height_of_beams;
773
774   if (max_gap_len > threshold)
775     {
776       int j = 0;
777       for (vsize i = 0; i < stems.size (); i++)
778         {
779           Grob *stem = stems[i];
780           Interval head_extents = head_extents_array[j++];
781
782           Direction d = (head_extents.center () < max_gap.center ())
783             ? UP : DOWN;
784
785           stem->set_property ("direction", scm_from_int (d));
786
787           head_extents.intersect (max_gap);
788           assert (head_extents.is_empty () || head_extents.length () < 1e-6);
789         }
790     }
791 }
792
793 /* Set stem's shorten property if unset.
794
795 TODO:
796 take some y-position (chord/beam/nearest?) into account
797 scmify forced-fraction
798
799 This is done in beam because the shorten has to be uniform over the
800 entire beam.
801 */
802
803
804
805 void
806 set_minimum_dy (Grob *me, Real *dy)
807 {
808   if (*dy)
809     {
810       /*
811         If dy is smaller than the smallest quant, we
812         get absurd direction-sign penalties.
813       */
814
815       Real ss = Staff_symbol_referencer::staff_space (me);
816       Real thickness = Beam::get_thickness (me) / ss;
817       Real slt = Staff_symbol_referencer::line_thickness (me) / ss;
818       Real sit = (thickness - slt) / 2;
819       Real inter = 0.5;
820       Real hang = 1.0 - (thickness - slt) / 2;
821
822       *dy = sign (*dy) * max (fabs (*dy),
823                               min (min (sit, inter), hang));
824     }
825 }
826
827   
828
829 MAKE_SCHEME_CALLBACK (Beam, calc_stem_shorten, 1)
830 SCM
831 Beam::calc_stem_shorten (SCM smob)
832 {
833   Grob *me = unsmob_grob (smob);
834   
835   /*
836     shortening looks silly for x staff beams
837   */
838   if (is_knee (me))
839     return scm_from_int (0);
840
841   Real forced_fraction = 1.0 * forced_stem_count (me)
842     / normal_stem_count (me);
843
844   int beam_count = get_beam_count (me);
845
846   SCM shorten_list = me->get_property ("beamed-stem-shorten");
847   if (shorten_list == SCM_EOL)
848     return scm_from_int (0);
849
850   Real staff_space = Staff_symbol_referencer::staff_space (me);
851
852   SCM shorten_elt
853     = robust_list_ref (beam_count -1, shorten_list);
854   Real shorten = scm_to_double (shorten_elt) * staff_space;
855
856   shorten *= forced_fraction;
857
858   
859   if (shorten)
860     return scm_from_double (shorten);
861
862   return scm_from_double (0.0);
863 }
864
865
866 Interval
867 Beam::no_visible_stem_positions (Grob *me, Interval default_value)
868 {
869   extract_grob_set (me, "stems", stems);
870   if (stems.empty ())
871     return default_value;
872   
873   Interval head_positions;
874   Slice multiplicity;
875   for (vsize i = 0; i < stems.size(); i++)
876     {
877       head_positions.unite (Stem::head_positions (stems[i]));
878       multiplicity.unite (Stem::beam_multiplicity (stems[i]));
879     }
880
881   Direction dir = get_grob_direction (me);
882   Real y = head_positions[dir]
883     * 0.5 * Staff_symbol_referencer::staff_space (me)
884     + dir * get_beam_translation (me) * (multiplicity.length () + 1);
885
886   y /= Staff_symbol_referencer::staff_space (me);
887   return Interval (y,y);
888 }
889
890
891 /*
892   Compute a first approximation to the beam slope.
893 */
894 MAKE_SCHEME_CALLBACK (Beam, calc_least_squares_positions, 2);
895 SCM
896 Beam::calc_least_squares_positions (SCM smob, SCM posns)
897 {
898   (void) posns;
899   
900   Grob *me = unsmob_grob (smob);
901
902   int count = normal_stem_count (me);
903   Interval pos (0,0);
904   if (count < 1)
905     return ly_interval2scm (no_visible_stem_positions (me, pos));
906
907   vector<Real> x_posns;
908   extract_grob_set (me, "normal-stems", stems);
909   Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
910   Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
911
912   Real my_y = me->relative_coordinate (commony, Y_AXIS);
913
914   Grob *fvs = first_normal_stem (me);
915   Grob *lvs = last_normal_stem (me);
916
917   Interval ideal (Stem::get_stem_info (fvs).ideal_y_
918                   + fvs->relative_coordinate (commony, Y_AXIS) - my_y,
919                   Stem::get_stem_info (lvs).ideal_y_
920                   + lvs->relative_coordinate (commony, Y_AXIS) - my_y);
921
922   Real x0 = first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
923   for (vsize i = 0; i < stems.size (); i++)
924     {
925       Grob *s = stems[i];
926
927       Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
928       x_posns.push_back (x);
929     }
930   Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS) - x0;
931
932   Real y = 0;
933   Real slope = 0;
934   Real dy = 0;
935   Real ldy = 0.0;
936   if (!ideal.delta ())
937     {
938       Interval chord (Stem::chord_start_y (stems[0]),
939                       Stem::chord_start_y (stems.back ()));
940
941       /* Simple beams (2 stems) on middle line should be allowed to be
942          slightly sloped.
943
944          However, if both stems reach middle line,
945          ideal[LEFT] == ideal[RIGHT] and ideal.delta () == 0.
946
947          For that case, we apply artificial slope */
948       if (!ideal[LEFT] && chord.delta () && count == 2)
949         {
950           /* FIXME. -> UP */
951           Direction d = (Direction) (sign (chord.delta ()) * UP);
952           pos[d] = get_thickness (me) / 2;
953           pos[-d] = -pos[d];
954         }
955       else
956         pos = ideal;
957
958       /*
959         For broken beams this doesn't work well. In this case, the
960         slope esp. of the first part of a broken beam should predict
961         where the second part goes.
962       */
963       ldy = pos[RIGHT] - pos[LEFT];
964     }
965   else
966     {
967       vector<Offset> ideals;
968       for (vsize i = 0; i < stems.size (); i++)
969         {
970           Grob *s = stems[i];
971           ideals.push_back (Offset (x_posns[i],
972                                Stem::get_stem_info (s).ideal_y_
973                                + s->relative_coordinate (commony, Y_AXIS)
974                                - my_y));
975         }
976
977       minimise_least_squares (&slope, &y, ideals);
978
979       dy = slope * dx;
980
981       set_minimum_dy (me, &dy);
982
983       ldy = dy;
984       pos = Interval (y, (y + dy));
985     }
986
987   /*
988     "position" is relative to the staff.
989   */
990   scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
991
992   me->set_property ("least-squares-dy",  scm_from_double (ldy));
993   return ly_interval2scm (pos);
994 }
995
996 /*
997   We can't combine with previous function, since check concave and
998   slope damping comes first.
999
1000   TODO: we should use the concaveness to control the amount of damping
1001   applied.
1002 */
1003 MAKE_SCHEME_CALLBACK (Beam, shift_region_to_valid, 2);
1004 SCM
1005 Beam::shift_region_to_valid (SCM grob, SCM posns)
1006 {
1007   Grob *me = unsmob_grob (grob);
1008   /*
1009     Code dup.
1010   */
1011   vector<Real> x_posns;
1012   extract_grob_set (me, "stems", stems);
1013   Grob *commonx = common_refpoint_of_array (stems, me, X_AXIS);
1014   Grob *commony = common_refpoint_of_array (stems, me, Y_AXIS);
1015
1016   Grob *fvs = first_normal_stem (me);
1017
1018   if (!fvs)
1019     return posns;
1020
1021   Real x0 = fvs->relative_coordinate (commonx, X_AXIS);
1022   for (vsize i = 0; i < stems.size (); i++)
1023     {
1024       Grob *s = stems[i];
1025
1026       Real x = s->relative_coordinate (commonx, X_AXIS) - x0;
1027       x_posns.push_back (x);
1028     }
1029
1030   Grob *lvs = last_normal_stem (me);
1031   if (!lvs)
1032     return posns;
1033
1034   Real dx = lvs->relative_coordinate (commonx, X_AXIS) - x0;
1035
1036   Drul_array<Real> pos = ly_scm2interval (posns);
1037
1038   scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1039
1040   Real dy = pos[RIGHT] - pos[LEFT];
1041   Real y = pos[LEFT];
1042   Real slope = dx ? (dy / dx) : 0.0;
1043
1044   /*
1045     Shift the positions so that we have a chance of finding good
1046     quants (i.e. no short stem failures.)
1047   */
1048   Interval feasible_left_point;
1049   feasible_left_point.set_full ();
1050   for (vsize i = 0; i < stems.size (); i++)
1051     {
1052       Grob *s = stems[i];
1053       if (Stem::is_invisible (s))
1054         continue;
1055
1056       Direction d = get_grob_direction (s);
1057
1058       Real left_y
1059         = Stem::get_stem_info (s).shortest_y_
1060         - slope * x_posns [i];
1061
1062       /*
1063         left_y is now relative to the stem S. We want relative to
1064         ourselves, so translate:
1065       */
1066       left_y
1067         += + s->relative_coordinate (commony, Y_AXIS)
1068         - me->relative_coordinate (commony, Y_AXIS);
1069
1070       Interval flp;
1071       flp.set_full ();
1072       flp[-d] = left_y;
1073
1074       feasible_left_point.intersect (flp);
1075     }
1076
1077   if (feasible_left_point.is_empty ())
1078     warning (_ ("no viable initial configuration found: may not find good beam slope"));
1079   else if (!feasible_left_point.contains (y))
1080     {
1081       const int REGION_SIZE = 2; // UGH UGH
1082       if (isinf (feasible_left_point[DOWN]))
1083         y = feasible_left_point[UP] - REGION_SIZE;
1084       else if (isinf (feasible_left_point[UP]))
1085         y = feasible_left_point[DOWN]+ REGION_SIZE;
1086       else
1087         y = feasible_left_point.center ();
1088     }
1089
1090   pos = Drul_array<Real> (y, (y + dy));
1091   scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1092
1093   return ly_interval2scm (pos);
1094 }
1095
1096 /* This neat trick is by Werner Lemberg,
1097    damped = tanh (slope)
1098    corresponds with some tables in [Wanske] CHECKME */
1099 MAKE_SCHEME_CALLBACK (Beam, slope_damping, 2);
1100 SCM
1101 Beam::slope_damping (SCM smob, SCM posns)
1102 {
1103   Grob *me = unsmob_grob (smob);
1104   Drul_array<Real> pos = ly_scm2interval (posns);
1105
1106   if (normal_stem_count (me) <= 1)
1107     return posns;
1108
1109   
1110   SCM s = me->get_property ("damping");
1111   Real damping = scm_to_double (s);
1112   Real concaveness = robust_scm2double (me->get_property ("concaveness"), 0.0);
1113   if (concaveness >= 10000)
1114     {
1115       pos[LEFT] = pos[RIGHT];
1116       me->set_property ("least-squares-dy", scm_from_double (0));
1117       damping = 0;
1118     }
1119   
1120   if (damping)
1121     {
1122       scale_drul (&pos, Staff_symbol_referencer::staff_space (me));
1123
1124       Real dy = pos[RIGHT] - pos[LEFT];
1125
1126       Grob *fvs = first_normal_stem (me);
1127       Grob *lvs = last_normal_stem (me);
1128
1129       Grob *commonx = fvs->common_refpoint (lvs, X_AXIS);
1130
1131       Real dx = last_normal_stem (me)->relative_coordinate (commonx, X_AXIS)
1132         - first_normal_stem (me)->relative_coordinate (commonx, X_AXIS);
1133
1134       Real slope = dy && dx ? dy / dx : 0;
1135
1136       slope = 0.6 * tanh (slope) / (damping + concaveness);
1137
1138       Real damped_dy = slope * dx;
1139
1140       set_minimum_dy (me, &damped_dy);
1141
1142       pos[LEFT] += (dy - damped_dy) / 2;
1143       pos[RIGHT] -= (dy - damped_dy) / 2;
1144
1145       scale_drul (&pos, 1 / Staff_symbol_referencer::staff_space (me));
1146     }
1147
1148   return ly_interval2scm (pos);
1149 }
1150
1151 /*
1152   Report slice containing the numbers that are both in (car BEAMING)
1153   and (cdr BEAMING)
1154 */
1155 Slice
1156 where_are_the_whole_beams (SCM beaming)
1157 {
1158   Slice l;
1159
1160   for (SCM s = scm_car (beaming); scm_is_pair (s); s = scm_cdr (s))
1161     {
1162       if (scm_c_memq (scm_car (s), scm_cdr (beaming)) != SCM_BOOL_F)
1163
1164         l.add_point (scm_to_int (scm_car (s)));
1165     }
1166
1167   return l;
1168 }
1169
1170 /* Return the Y position of the stem-end, given the Y-left, Y-right
1171    in POS for stem S.  This Y position is relative to S. */
1172 Real
1173 Beam::calc_stem_y (Grob *me, Grob *stem, Grob **common,
1174                    Real xl, Real xr, Direction feather_dir, 
1175                    Drul_array<Real> pos, bool french)
1176 {
1177   Real beam_translation = get_beam_translation (me);
1178   Direction stem_dir = get_grob_direction (stem);
1179
1180   Real dx = xr - xl;
1181   Real relx = dx ? (stem->relative_coordinate (common[X_AXIS], X_AXIS) - xl)/dx : 0;
1182   Real xdir = 2*relx-1;
1183
1184   Real stem_y = linear_combination(pos, xdir);
1185
1186   SCM beaming = stem->get_property ("beaming");
1187
1188   Slice beam_slice (french
1189                     ? where_are_the_whole_beams (beaming)
1190                     : Stem::beam_multiplicity (stem));
1191   if (beam_slice.is_empty ())
1192     beam_slice = Slice (0,0);
1193   Interval beam_multiplicity(beam_slice[LEFT],
1194                              beam_slice[RIGHT]);
1195
1196   /*
1197     feather dir = 1 , relx 0->1 : factor 0 -> 1
1198     feather dir = 0 , relx 0->1 : factor 1 -> 1    
1199     feather dir = -1, relx 0->1 : factor 1 -> 0    
1200    */
1201   Real feather_factor = 1;
1202   if (feather_dir > 0)
1203     feather_factor = relx;
1204   else if (feather_dir < 0)
1205     feather_factor = 1 - relx;
1206   
1207   stem_y += feather_factor * beam_translation
1208     * beam_multiplicity[Direction(((french) ? DOWN : UP)*stem_dir)];
1209   Real id = me->relative_coordinate (common[Y_AXIS], Y_AXIS)
1210     - stem->relative_coordinate (common[Y_AXIS], Y_AXIS);
1211
1212   return stem_y + id;
1213 }
1214
1215 /*
1216   Hmm.  At this time, beam position and slope are determined.  Maybe,
1217   stem directions and length should set to relative to the chord's
1218   position of the beam.  */
1219 MAKE_SCHEME_CALLBACK (Beam, set_stem_lengths, 1); 
1220 SCM
1221 Beam::set_stem_lengths (SCM smob)
1222 {
1223   Grob *me = unsmob_grob (smob);
1224
1225   /* trigger callbacks. */
1226   (void) me->get_property ("direction");
1227   (void) me->get_property ("beaming");
1228
1229   SCM posns = me->get_property ("positions");
1230   
1231   extract_grob_set (me, "stems", stems);
1232   if (!stems.size ())
1233     return posns;
1234
1235   Grob *common[2];
1236   for (int a = 2; a--;)
1237     common[a] = common_refpoint_of_array (stems, me, Axis (a));
1238
1239   Drul_array<Real> pos = ly_scm2realdrul (posns);
1240   Real staff_space = Staff_symbol_referencer::staff_space (me);
1241   scale_drul (&pos, staff_space);
1242
1243   bool gap = false;
1244   Real thick = 0.0;
1245   if (robust_scm2int (me->get_property ("gap-count"), 0))
1246     {
1247       gap = true;
1248       thick = get_thickness (me);
1249     }
1250
1251   Grob *fvs = first_normal_stem (me);
1252   Grob *lvs = last_normal_stem (me);
1253
1254   Real xl = fvs ? fvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1255   Real xr = lvs ? lvs->relative_coordinate (common[X_AXIS], X_AXIS) : 0.0;
1256   Direction feather_dir = to_dir (me->get_property ("grow-direction"));
1257
1258   for (vsize i = 0; i < stems.size (); i++)
1259     {
1260       Grob *s = stems[i];
1261
1262       bool french = to_boolean (s->get_property ("french-beaming"));
1263       Real stem_y = calc_stem_y (me, s, common,
1264                                  xl, xr, feather_dir,
1265                                  pos, french && s != lvs && s!= fvs);
1266
1267       /*
1268         Make the stems go up to the end of the beam. This doesn't matter
1269         for normal beams, but for tremolo beams it looks silly otherwise.
1270       */
1271       if (gap
1272           && !Stem::is_invisible (s))
1273         stem_y += thick * 0.5 * get_grob_direction (s);
1274
1275       /*
1276         Do set_stemend for invisible stems too, so tuplet brackets
1277         have a reference point for sloping
1278        */
1279       Stem::set_stemend (s, 2 * stem_y / staff_space);
1280     }
1281
1282   return posns;
1283 }
1284
1285 void
1286 Beam::set_beaming (Grob *me, Beaming_pattern const *beaming)
1287 {
1288   extract_grob_set (me, "stems", stems);
1289
1290   Direction d = LEFT;
1291   for (vsize i = 0; i < stems.size (); i++)
1292     {
1293       /*
1294         Don't overwrite user settings.
1295       */
1296       do
1297         {
1298           Grob *stem = stems[i];
1299           SCM beaming_prop = stem->get_property ("beaming");
1300           if (beaming_prop == SCM_EOL
1301               || index_get_cell (beaming_prop, d) == SCM_EOL)
1302             {
1303               int count = beaming->beamlet_count (i, d);
1304               if (i > 0
1305                   && i + 1 < stems.size ()
1306                   && Stem::is_invisible (stem))
1307                 count = min (count, beaming->beamlet_count (i,-d));
1308
1309               if ( ((i == 0 && d == LEFT)
1310                     || (i == stems.size ()-1 && d == RIGHT))
1311                    && stems.size () > 1
1312                    && to_boolean (me->get_property ("clip-edges")))
1313                 count = 0;
1314
1315               Stem::set_beaming (stem, count, d);
1316             }
1317         }
1318       while (flip (&d) != LEFT);
1319     }
1320 }
1321
1322 int
1323 Beam::forced_stem_count (Grob *me)
1324 {
1325   extract_grob_set (me, "normal-stems", stems);
1326
1327   int f = 0;
1328   for (vsize i = 0; i < stems.size (); i++)
1329     {
1330       Grob *s = stems[i];
1331
1332       /* I can imagine counting those boundaries as a half forced stem,
1333          but let's count them full for now. */
1334       Direction defdir = to_dir (s->get_property ("default-direction"));
1335       
1336       if (abs (Stem::chord_start_y (s)) > 0.1
1337           && defdir
1338           && get_grob_direction (s) != defdir)
1339         f++;
1340     }
1341   return f;
1342 }
1343
1344 int
1345 Beam::normal_stem_count (Grob *me)
1346 {
1347   extract_grob_set (me, "normal-stems", stems);
1348   return stems.size ();
1349 }
1350
1351 Grob *
1352 Beam::first_normal_stem (Grob *me)
1353 {
1354   extract_grob_set (me, "normal-stems", stems);
1355   return stems.size () ? stems[0] : 0;
1356 }
1357
1358 Grob *
1359 Beam::last_normal_stem (Grob *me)
1360 {
1361   extract_grob_set (me, "normal-stems", stems);
1362   return stems.size () ? stems.back () : 0;
1363 }
1364
1365 /*
1366   [TODO]
1367
1368   handle rest under beam (do_post: beams are calculated now)
1369   what about combination of collisions and rest under beam.
1370
1371   Should lookup
1372
1373   rest -> stem -> beam -> interpolate_y_position ()
1374 */
1375 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Beam, rest_collision_callback, 2, 1, "");
1376 SCM
1377 Beam::rest_collision_callback (SCM smob, SCM prev_offset)
1378 {
1379   Grob *rest = unsmob_grob (smob);
1380   if (scm_is_number (rest->get_property ("staff-position")))
1381     return scm_from_int (0);
1382
1383   Real offset = robust_scm2double (prev_offset, 0.0);
1384   
1385   Grob *st = unsmob_grob (rest->get_object ("stem"));
1386   Grob *stem = st;
1387   if (!stem)
1388     return scm_from_double (0.0);
1389   Grob *beam = unsmob_grob (stem->get_object ("beam"));
1390   if (!beam
1391       || !Beam::has_interface (beam)
1392       || !Beam::normal_stem_count (beam))
1393     return scm_from_double (0.0);
1394
1395   Drul_array<Real> pos (robust_scm2drul (beam->get_property ("positions"),
1396                                          Drul_array<Real> (0,0)));
1397
1398   Real staff_space = Staff_symbol_referencer::staff_space (rest);
1399
1400   scale_drul (&pos, staff_space);
1401
1402   Real dy = pos[RIGHT] - pos[LEFT];
1403
1404   Drul_array<Grob*> visible_stems (first_normal_stem (beam),
1405                                    last_normal_stem (beam));
1406   extract_grob_set (beam, "stems", stems);
1407   
1408   Grob *common = common_refpoint_of_array (stems, beam, X_AXIS);
1409   
1410   Real x0 = visible_stems[LEFT]->relative_coordinate (common, X_AXIS);
1411   Real dx = visible_stems[RIGHT]->relative_coordinate (common, X_AXIS) - x0;
1412   Real slope = dy && dx ? dy / dx : 0;
1413
1414   Direction d = get_grob_direction (stem);
1415   Real stem_y = pos[LEFT]
1416     + (stem->relative_coordinate (common, X_AXIS) - x0) * slope;
1417
1418   Real beam_translation = get_beam_translation (beam);
1419   Real beam_thickness = Beam::get_thickness (beam);
1420
1421   /*
1422     TODO: this is not strictly correct for 16th knee beams.
1423   */
1424   int beam_count
1425     = Stem::beam_multiplicity (stem).length () + 1;
1426
1427   Real height_of_my_beams = beam_thickness / 2
1428     + (beam_count - 1) * beam_translation;
1429   Real beam_y = stem_y - d * height_of_my_beams;
1430
1431   Grob *common_y = rest->common_refpoint (beam, Y_AXIS);
1432
1433   /*
1434     TODO: this is dubious, because this call needs the info we're
1435     computing right now.
1436    */
1437   Interval rest_extent = rest->extent (common_y, Y_AXIS);
1438   rest_extent.translate (offset);
1439   
1440   Real rest_dim = rest_extent[d];
1441   Real minimum_distance
1442     = staff_space * (robust_scm2double (stem->get_property ("stemlet-length"), 0.0)
1443                      + robust_scm2double (rest->get_property ("minimum-distance"), 0.0));
1444
1445   Real shift = d * min (d * (beam_y - d * minimum_distance - rest_dim), 0.0);
1446
1447   shift /= staff_space;
1448   Real rad = Staff_symbol_referencer::line_count (rest) * staff_space / 2;
1449
1450   /* Always move discretely by half spaces */
1451   shift = ceil (fabs (shift * 2.0)) / 2.0 * sign (shift);
1452
1453   /* Inside staff, move by whole spaces*/
1454   if ((rest_extent[d] + staff_space * shift) * d
1455       < rad
1456       || (rest_extent[-d] + staff_space * shift) * -d
1457       < rad)
1458     shift = ceil (fabs (shift)) * sign (shift);
1459
1460   return scm_from_double (offset + staff_space * shift);
1461 }
1462
1463 bool
1464 Beam::is_knee (Grob *me)
1465 {
1466   SCM k = me->get_property ("knee");
1467   if (scm_is_bool (k))
1468     return ly_scm2bool (k);
1469
1470   bool knee = false;
1471   int d = 0;
1472   extract_grob_set (me, "stems", stems);
1473   for (vsize i = stems.size (); i--;)
1474     {
1475       Direction dir = get_grob_direction (stems[i]);
1476       if (d && d != dir)
1477         {
1478           knee = true;
1479           break;
1480         }
1481       d = dir;
1482     }
1483
1484   me->set_property ("knee", ly_bool2scm (knee));
1485
1486   return knee;
1487 }
1488
1489 bool
1490 Beam::is_cross_staff (Grob *me)
1491 {
1492   extract_grob_set (me, "stems", stems);
1493   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
1494   for (vsize i = 0; i < stems.size (); i++)
1495     if (Staff_symbol_referencer::get_staff_symbol (stems[i]) != staff_symbol)
1496       return true;
1497   return false;
1498 }
1499
1500 MAKE_SCHEME_CALLBACK (Beam, calc_cross_staff, 1)
1501 SCM
1502 Beam::calc_cross_staff (SCM smob)
1503 {
1504   return scm_from_bool (is_cross_staff (unsmob_grob (smob)));
1505 }
1506
1507 int
1508 Beam::get_direction_beam_count (Grob *me, Direction d)
1509 {
1510   extract_grob_set (me, "stems", stems);
1511   int bc = 0;
1512
1513   for (vsize i = stems.size (); i--;)
1514     {
1515       /*
1516         Should we take invisible stems into account?
1517       */
1518       if (get_grob_direction (stems[i]) == d)
1519         bc = max (bc, (Stem::beam_multiplicity (stems[i]).length () + 1));
1520     }
1521
1522   return bc;
1523 }
1524
1525 ADD_INTERFACE (Beam,
1526                "A beam.\n"
1527                "\n"
1528                "The @code{thickness} property is the weight of beams,"
1529                " measured in staffspace.  The @code{direction} property is"
1530                " not user-serviceable.  Use the @code{direction} property"
1531                " of @code{Stem} instead.",
1532                
1533                /* properties */
1534                "annotation "
1535                "auto-knee-gap "
1536                "beamed-stem-shorten "
1537                "beaming "
1538                "break-overshoot "
1539                "clip-edges "
1540                "concaveness "
1541                "damping "
1542                "details "
1543                "direction "
1544                "gap "
1545                "gap-count "
1546                "grow-direction "
1547                "inspect-quants "
1548                "knee "
1549                "length-fraction "
1550                "least-squares-dy "
1551                "neutral-direction "
1552                "normal-stems "
1553                "positions "
1554                "quant-score "
1555                "quantized-positions "
1556                "shorten "
1557                "stems "
1558                "thickness "
1559                );