]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-bracket.cc
Web-ja: update introduction
[lilypond.git] / lily / tuplet-bracket.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22   TODO:
23
24   - tuplet bracket should probably be subject to the same rules as
25   beam sloping/quanting.
26
27   - There is no support for kneed brackets, or nested brackets.
28
29   - number placement for parallel beams should be much more advanced:
30   for sloped beams some extra horizontal offset must be introduced.
31
32   - number placement is usually done over the center note, not the
33   graphical center.
34 */
35
36 /*
37   TODO: quantise, we don't want to collide with staff lines.
38   (or should we be above staff?)
39
40   todo: handle breaking elegantly.
41 */
42
43 #include "tuplet-bracket.hh"
44
45 #include "bracket.hh"
46 #include "axis-group-interface.hh"
47 #include "line-interface.hh"
48 #include "beam.hh"
49 #include "warn.hh"
50 #include "output-def.hh"
51 #include "font-interface.hh"
52 #include "text-interface.hh"
53 #include "stem.hh"
54 #include "note-column.hh"
55 #include "pointer-group-interface.hh"
56 #include "directional-element-interface.hh"
57 #include "skyline.hh"
58 #include "spanner.hh"
59 #include "staff-symbol-referencer.hh"
60 #include "lookup.hh"
61 #include "paper-column.hh"
62 #include "moment.hh"
63
64 static Item *
65 get_x_bound_item (Grob *me_grob, Direction hdir, Direction my_dir)
66 {
67   Spanner *me = dynamic_cast<Spanner *> (me_grob);
68   Item *g = me->get_bound (hdir);
69   if (has_interface<Note_column> (g)
70       && Note_column::get_stem (g)
71       && Note_column::dir (g) == my_dir)
72     g = Note_column::get_stem (g);
73
74   return g;
75 }
76
77 void
78 flatten_number_pair_property (Grob *me, Direction xdir, SCM sym)
79 {
80   Drul_array<Real> zero (0, 0);
81   Drul_array<Real> pair
82     = robust_scm2drul (me->get_property (sym), zero);
83   pair[xdir] = 0.0;
84
85   me->set_property (sym, ly_interval2scm (pair));
86 }
87
88 /*
89   Return beam that encompasses the span of the tuplet bracket.
90 */
91 Grob *
92 Tuplet_bracket::parallel_beam (Grob *me_grob, vector<Grob *> const &cols,
93                                bool *equally_long)
94 {
95   Spanner *me = dynamic_cast<Spanner *> (me_grob);
96
97   Item *left = me->get_bound (LEFT);
98   Item *right = me->get_bound (RIGHT);
99   if (!left || left->break_status_dir ()
100       || !right || right->break_status_dir ())
101     return 0;
102
103   Drul_array<Grob *> stems (Note_column::get_stem (cols[0]),
104                             Note_column::get_stem (cols.back ()));
105
106   if (!stems[RIGHT]
107       || !stems[LEFT]
108       || (dynamic_cast<Item *> (stems[RIGHT])->get_column ()
109           != me->get_bound (RIGHT)->get_column ()))
110     return 0;
111
112   Drul_array<Grob *> beams;
113   for (LEFT_and_RIGHT (d))
114     beams[d] = stems[d] ? Stem::get_beam (stems[d]) : 0;
115
116   *equally_long = false;
117   if (! (beams[LEFT] && (beams[LEFT] == beams[RIGHT]) && !me->is_broken ()))
118     return 0;
119
120   extract_grob_set (beams[LEFT], "stems", beam_stems);
121   if (beam_stems.size () == 0)
122     {
123       programming_error ("beam under tuplet bracket has no stems");
124       *equally_long = 0;
125       return 0;
126     }
127
128   *equally_long
129     = (beam_stems[0] == stems[LEFT]
130        && beam_stems.back () == stems[RIGHT]);
131   return beams[LEFT];
132 }
133
134 MAKE_SCHEME_CALLBACK (Tuplet_bracket, calc_connect_to_neighbors, 1);
135 SCM
136 Tuplet_bracket::calc_connect_to_neighbors (SCM smob)
137 {
138   Spanner *me = unsmob<Spanner> (smob);
139
140   Direction dir = get_grob_direction (me);
141   Drul_array<Item *> bounds (get_x_bound_item (me, LEFT, dir),
142                              get_x_bound_item (me, RIGHT, dir));
143
144   Drul_array<bool> connect_to_other (false, false);
145   for (LEFT_and_RIGHT (d))
146     {
147       Direction break_dir = bounds[d]->break_status_dir ();
148       Spanner *orig_spanner = dynamic_cast<Spanner *> (me->original ());
149       vsize neighbor_idx = me->get_break_index () - break_dir;
150       if (break_dir
151           && d == RIGHT
152           && neighbor_idx < orig_spanner->broken_intos_.size ())
153         {
154           Grob *neighbor = orig_spanner->broken_intos_[neighbor_idx];
155
156           /* trigger possible suicide*/
157           (void) neighbor->get_property ("positions");
158         }
159
160       connect_to_other[d]
161         = (break_dir
162            && neighbor_idx < orig_spanner->broken_intos_.size ()
163            && orig_spanner->broken_intos_[neighbor_idx]->is_live ());
164     }
165
166   if (connect_to_other[LEFT] || connect_to_other[RIGHT])
167     return scm_cons (scm_from_bool (connect_to_other[LEFT]),
168                      scm_from_bool (connect_to_other[RIGHT]));
169
170   return SCM_EOL;
171 }
172
173 Grob *
174 Tuplet_bracket::get_common_x (Spanner *me)
175 {
176   extract_grob_set (me, "note-columns", columns);
177
178   Grob *commonx = common_refpoint_of_array (columns, me, X_AXIS);
179   commonx = commonx->common_refpoint (me->get_bound (LEFT), X_AXIS);
180   commonx = commonx->common_refpoint (me->get_bound (RIGHT), X_AXIS);
181
182   return commonx;
183 }
184
185 MAKE_SCHEME_CALLBACK (Tuplet_bracket, calc_x_positions, 1)
186 SCM
187 Tuplet_bracket::calc_x_positions (SCM smob)
188 {
189   Spanner *me = unsmob<Spanner> (smob);
190   extract_grob_set (me, "note-columns", columns);
191
192   Grob *commonx = get_common_x (me);
193   Direction dir = get_grob_direction (me);
194
195   Drul_array<Item *> bounds;
196   bounds[LEFT] = get_x_bound_item (me, LEFT, dir);
197   bounds[RIGHT] = get_x_bound_item (me, RIGHT, dir);
198
199   Drul_array<bool> connect_to_other
200     = robust_scm2booldrul (me->get_property ("connect-to-neighbor"),
201                            Drul_array<bool> (false, false));
202
203   Interval x_span;
204   for (LEFT_and_RIGHT (d))
205     {
206       x_span[d] = Axis_group_interface::generic_bound_extent (bounds[d], commonx, X_AXIS)[d];
207
208       if (connect_to_other[d])
209         {
210           Interval overshoot (robust_scm2drul (me->get_property ("break-overshoot"),
211                                                Interval (-0.5, 0.0)));
212
213           if (d == RIGHT)
214             x_span[d] += d * overshoot[d];
215           else
216             x_span[d] = (bounds[d]->break_status_dir ()
217                          ? Axis_group_interface::generic_bound_extent (bounds[d], commonx, X_AXIS)[-d]
218                          : robust_relative_extent (bounds[d], commonx, X_AXIS)[-d])
219                         - overshoot[LEFT];
220         }
221
222       else if (d == RIGHT
223                && (columns.empty ()
224                    || (bounds[d]->get_column ()
225                        != dynamic_cast<Item *> (columns.back ())->get_column ())))
226         {
227           /*
228             We're connecting to a column, for the last bit of a broken
229             fullLength bracket.
230           */
231           Real padding
232             = robust_scm2double (me->get_property ("full-length-padding"), 1.0);
233
234           if (bounds[d]->break_status_dir ())
235             padding = 0.0;
236
237           Real coord = bounds[d]->relative_coordinate (commonx, X_AXIS);
238           if (to_boolean (me->get_property ("full-length-to-extent")))
239             coord = robust_relative_extent (bounds[d], commonx, X_AXIS)[LEFT];
240
241           coord = max (coord, x_span[LEFT]);
242
243           x_span[d] = coord - padding;
244         }
245     }
246
247   return ly_interval2scm (x_span - me->get_bound (LEFT)->relative_coordinate (commonx, X_AXIS));
248 }
249
250 /*
251   TODO:
252
253   in the case that there is no bracket, but there is a (single) beam,
254   follow beam precisely for determining tuplet number location.
255 */
256 MAKE_SCHEME_CALLBACK (Tuplet_bracket, print, 1);
257 SCM
258 Tuplet_bracket::print (SCM smob)
259 {
260   Spanner *me = unsmob<Spanner> (smob);
261   Stencil mol;
262
263   extract_grob_set (me, "note-columns", columns);
264   bool equally_long = false;
265   Grob *par_beam = parallel_beam (me, columns, &equally_long);
266
267   bool bracket_visibility = !(par_beam && equally_long); // Flag, print/don't print tuplet bracket.
268   /*
269     FIXME: The type of this prop is sucky.
270   */
271   SCM bracket_vis_prop = me->get_property ("bracket-visibility");
272   bool bracket_prop = ly_scm2bool (bracket_vis_prop); // Flag, user has set bracket-visibility prop.
273   bool bracket = scm_is_eq (bracket_vis_prop, ly_symbol2scm ("if-no-beam"));
274   if (scm_is_bool (bracket_vis_prop))
275     bracket_visibility = bracket_prop;
276   else if (bracket)
277     bracket_visibility = !par_beam;
278
279   /*
280     Don't print a tuplet bracket and number if
281     no X or Y positions were calculated.
282   */
283   SCM scm_x_span = me->get_property ("X-positions");
284   SCM scm_positions = me->get_property ("positions");
285   if (!scm_is_pair (scm_x_span) || !scm_is_pair (scm_positions))
286     {
287       me->suicide ();
288       return SCM_EOL;
289     }
290   /*  if the tuplet does not span any time, i.e. a single-note tuplet, hide
291       the bracket, but still let the number be displayed.
292       Only do this if the user has not explicitly specified bracket-visibility = #t.
293   */
294   if (!to_boolean (bracket_vis_prop)
295       && (robust_scm2moment (me->get_bound (LEFT)->get_column ()->get_property ("when"), Moment (0))
296           == robust_scm2moment (me->get_bound (RIGHT)->get_column ()->get_property ("when"), Moment (0))))
297     bracket_visibility = false;
298
299   Interval x_span = robust_scm2interval (scm_x_span, Interval (0.0, 0.0));
300   Interval positions = robust_scm2interval (scm_positions, Interval (0.0, 0.0));
301
302   Drul_array<Offset> points;
303   for (LEFT_and_RIGHT (d))
304     points[d] = Offset (x_span[d], positions[d]);
305
306   Output_def *pap = me->layout ();
307
308   Grob *number_grob = unsmob<Grob> (me->get_object ("tuplet-number"));
309
310   /*
311     Don't print the bracket when it would be smaller than the number.
312     ...Unless the user has coded bracket-visibility = #t, that is.
313   */
314   Real gap = 0.;
315   if (bracket_visibility && number_grob)
316     {
317       Interval ext = number_grob->extent (number_grob, X_AXIS);
318       if (!ext.is_empty ())
319         {
320           gap = ext.length () + 1.0;
321
322           if ((0.75 * x_span.length () < gap) && !bracket_prop)
323             bracket_visibility = false;
324         }
325     }
326
327   if (bracket_visibility)
328     {
329       Drul_array<Real> zero (0, 0);
330       Real ss = Staff_symbol_referencer::staff_space (me);
331       Drul_array<Real> height
332         = robust_scm2drul (me->get_property ("edge-height"), zero);
333       Drul_array<Real> flare
334         = robust_scm2drul (me->get_property ("bracket-flare"), zero);
335       Drul_array<Real> shorten
336         = robust_scm2drul (me->get_property ("shorten-pair"), zero);
337       Drul_array<Stencil> edge_stencils;
338
339       Direction dir = get_grob_direction (me);
340
341       scale_drul (&height, -ss * dir);
342       scale_drul (&flare, ss);
343       scale_drul (&shorten, ss);
344
345       Drul_array<bool> connect_to_other
346         = robust_scm2booldrul (me->get_property ("connect-to-neighbor"),
347                                Drul_array<bool> (false, false));
348
349       for (LEFT_and_RIGHT (d))
350         {
351           if (connect_to_other[d])
352             {
353               height[d] = 0.0;
354               flare[d] = 0.0;
355               shorten[d] = 0.0;
356
357               SCM edge_text = me->get_property ("edge-text");
358
359               if (scm_is_pair (edge_text))
360                 {
361                   SCM properties = Font_interface::text_font_alist_chain (me);
362                   SCM text = index_get_cell (edge_text, d);
363                   if (Text_interface::is_markup (text))
364                     {
365                       SCM t
366                         = Text_interface::interpret_markup (pap->self_scm (),
367                                                             properties, text);
368
369                       Stencil *edge_text = unsmob<Stencil> (t);
370                       edge_text->translate_axis (x_span[d] - x_span[LEFT],
371                                                  X_AXIS);
372                       edge_stencils[d] = *edge_text;
373                     }
374                 }
375             }
376         }
377
378       Stencil brack =
379         Bracket::make_bracket (
380           me, Y_AXIS, points[RIGHT] - points[LEFT], height,
381           /*
382             0.1 = more space at right due to italics
383             TODO: use italic correction of font.
384           */
385           Interval (-0.5, 0.5) * gap + 0.1,
386           flare, shorten);
387
388       for (LEFT_and_RIGHT (d))
389         {
390           if (!edge_stencils[d].is_empty ())
391             brack.add_stencil (edge_stencils[d]);
392         }
393
394       mol.add_stencil (brack);
395     }
396
397   mol.translate (points[LEFT]);
398   return mol.smobbed_copy ();
399 }
400
401 void
402 Tuplet_bracket::get_bounds (Grob *me, Grob **left, Grob **right)
403 {
404   extract_grob_set (me, "note-columns", columns);
405   vsize l = 0;
406   while (l < columns.size () && Note_column::has_rests (columns[l]))
407     l++;
408
409   vsize r = columns.size ();
410   while (r > l && Note_column::has_rests (columns[r - 1]))
411     r--;
412
413   *left = *right = 0;
414
415   if (l < r)
416     {
417       *left = columns[l];
418       *right = columns[r - 1];
419     }
420 }
421
422 /*
423   use first -> last note for slope, and then correct for disturbing
424   notes in between.  */
425 void
426 Tuplet_bracket::calc_position_and_height (Grob *me_grob, Real *offset, Real *dy)
427 {
428   Spanner *me = dynamic_cast<Spanner *> (me_grob);
429
430   extract_grob_set (me, "note-columns", columns);
431   extract_grob_set (me, "tuplets", tuplets);
432
433   Grob *commony = common_refpoint_of_array (columns, me, Y_AXIS);
434   commony = common_refpoint_of_array (tuplets, commony, Y_AXIS);
435   if (Grob *st = Staff_symbol_referencer::get_staff_symbol (me))
436     commony = st->common_refpoint (commony, Y_AXIS);
437   Real my_offset = me->relative_coordinate (commony, Y_AXIS);
438
439   Grob *commonx = get_common_x (me);
440   commonx = common_refpoint_of_array (tuplets, commonx, Y_AXIS);
441
442   Interval staff;
443   Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
444
445   /* staff-padding doesn't work correctly on cross-staff tuplets
446      because it only considers one staff symbol. Until this works,
447      disable it. */
448   if (st && !to_boolean (me->get_property ("cross-staff")))
449     {
450       Real pad = robust_scm2double (me->get_property ("staff-padding"), -1.0);
451       if (pad >= 0.0)
452         {
453           staff = st->extent (commony, Y_AXIS) - my_offset;
454           staff.widen (pad);
455         }
456     }
457
458   Direction dir = get_grob_direction (me);
459
460   bool equally_long = false;
461   Grob *par_beam = parallel_beam (me, columns, &equally_long);
462
463   Item *lgr = get_x_bound_item (me, LEFT, dir);
464   Item *rgr = get_x_bound_item (me, RIGHT, dir);
465   Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
466   Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
467   bool follow_beam = par_beam
468                      && get_grob_direction (par_beam) == dir
469                      && !Beam::is_knee (par_beam);
470
471   vector<Offset> points;
472   if (columns.size ()
473       && follow_beam
474       && Note_column::get_stem (columns[0])
475       && Note_column::get_stem (columns.back ()))
476     {
477       Drul_array<Grob *> stems (Note_column::get_stem (columns[0]),
478                                 Note_column::get_stem (columns.back ()));
479
480       Interval poss;
481       for (LEFT_and_RIGHT (side))
482         {
483           // Trigger setting of stem lengths if necessary.
484           if (Grob *beam = Stem::get_beam (stems[side]))
485             (void) beam->get_property ("quantized-positions");
486           poss[side] = stems[side]->extent (stems[side], Y_AXIS)[get_grob_direction (stems[side])]
487                        + stems[side]->parent_relative (commony, Y_AXIS);
488         }
489
490       *dy = poss[RIGHT] - poss[LEFT];
491       points.push_back (Offset (stems[LEFT]->relative_coordinate (commonx, X_AXIS) - x0, poss[LEFT]));
492       points.push_back (Offset (stems[RIGHT]->relative_coordinate (commonx, X_AXIS) - x0, poss[RIGHT]));
493     }
494   else
495     {
496       /*
497         Use outer non-rest columns to determine slope
498       */
499       Grob *left_col = 0;
500       Grob *right_col = 0;
501       get_bounds (me, &left_col, &right_col);
502       if (left_col && right_col)
503         {
504           Interval rv = Note_column::cross_staff_extent (right_col, commony);
505           Interval lv = Note_column::cross_staff_extent (left_col, commony);
506           rv.unite (staff);
507           lv.unite (staff);
508
509           Real graphical_dy = rv[dir] - lv[dir];
510
511           Slice ls = Note_column::head_positions_interval (left_col);
512           Slice rs = Note_column::head_positions_interval (right_col);
513
514           Interval musical_dy;
515           musical_dy[UP] = rs[UP] - ls[UP];
516           musical_dy[DOWN] = rs[DOWN] - ls[DOWN];
517           if (sign (musical_dy[UP]) != sign (musical_dy[DOWN]))
518             *dy = 0.0;
519           else if (sign (graphical_dy) != sign (musical_dy[DOWN]))
520             *dy = 0.0;
521           else
522             *dy = graphical_dy;
523         }
524       else
525         *dy = 0;
526
527       for (vsize i = 0; i < columns.size (); i++)
528         {
529           Interval note_ext = Note_column::cross_staff_extent (columns[i],
530                                                                commony);
531           Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
532
533           points.push_back (Offset (x, note_ext[dir]));
534         }
535     }
536
537   if (!follow_beam)
538     {
539       points.push_back (Offset (x0 - x0, staff[dir]));
540       points.push_back (Offset (x1 - x0, staff[dir]));
541     }
542
543   /*
544     This is a slight hack. We compute two encompass points from the
545     bbox of the smaller tuplets.
546
547     We assume that the smaller bracket is 1.0 space high.
548   */
549   Real ss = Staff_symbol_referencer::staff_space (me);
550   for (vsize i = 0; i < tuplets.size (); i++)
551     {
552       Interval tuplet_x (tuplets[i]->extent (commonx, X_AXIS));
553       Interval tuplet_y (tuplets[i]->extent (commony, Y_AXIS));
554
555       if (!tuplets[i]->is_live ())
556         continue;
557
558       Drul_array<Real> positions
559         = robust_scm2interval (tuplets[i]->get_property ("positions"),
560                                Interval (0, 0));
561
562       Real other_dy = positions[RIGHT] - positions[LEFT];
563
564       for (LEFT_and_RIGHT (d))
565         {
566           Real y
567             = tuplet_y.linear_combination (d * sign (other_dy));
568
569           /*
570             We don't take padding into account for nested tuplets.
571             the edges can come very close to the stems, likewise for
572             nested tuplets?
573           */
574
575           points.push_back (Offset (tuplet_x[d] - x0, y));
576         }
577
578       // Check for number-on-bracket collisions
579       Grob *number = unsmob<Grob> (tuplets[i]->get_object ("tuplet-number"));
580       if (number)
581         {
582           Interval x_ext = robust_relative_extent (number, commonx, X_AXIS);
583           Interval y_ext = robust_relative_extent (number, commony, Y_AXIS);
584           points.push_back (Offset (x_ext.center () - x0, y_ext[dir]));
585         }
586     }
587
588   if (to_boolean (me->get_property ("avoid-scripts"))
589       && !scm_is_number (me->get_property ("outside-staff-priority")))
590     {
591       extract_grob_set (me, "scripts", scripts);
592       for (vsize i = 0; i < scripts.size (); i++)
593         {
594           if (!scripts[i]->is_live ())
595             continue;
596           if (scm_is_number (scripts[i]->get_property ("outside-staff-priority")))
597             continue;
598
599           // assume that if a script is avoiding slurs, it should not get placed
600           // under a tuplet bracket
601           if (unsmob<Grob> (scripts[i]->get_object ("slur")))
602             continue;
603
604           Interval script_x
605             = robust_relative_extent (scripts[i], commonx, X_AXIS);
606           Interval script_y
607             = robust_relative_extent (scripts[i], commony, Y_AXIS);
608
609           points.push_back (Offset (script_x.center () - x0,
610                                     script_y[dir]));
611         }
612     }
613
614   *offset = -dir * infinity_f;
615   Real factor = (columns.size () > 1) ? 1 / (x1 - x0) : 1.0;
616   for (vsize i = 0; i < points.size (); i++)
617     {
618       Real x = points[i][X_AXIS];
619       Real tuplety = (*dy) * x * factor + my_offset;
620
621       if (points[i][Y_AXIS] * dir > (*offset + tuplety) * dir)
622         *offset = points[i][Y_AXIS] - tuplety;
623     }
624
625   *offset += scm_to_double (me->get_property ("padding")) * dir;
626
627   /*
628     horizontal brackets should not collide with staff lines.
629
630     This doesn't seem to support cross-staff tuplets atm.
631   */
632   if (*dy == 0)
633     {
634       // quantize, then do collision check.
635       *offset /= 0.5 * ss;
636
637       Interval staff_span = Staff_symbol_referencer::staff_span (me);
638       if (staff_span.contains (*offset))
639         {
640           *offset = rint (*offset);
641           if (Staff_symbol_referencer::on_line (me, int (*offset)))
642             *offset += dir;
643         }
644
645       *offset *= 0.5 * ss;
646     }
647 }
648
649 MAKE_SCHEME_CALLBACK (Tuplet_bracket, calc_direction, 1);
650 SCM
651 Tuplet_bracket::calc_direction (SCM smob)
652 {
653   Grob *me = unsmob<Grob> (smob);
654   Direction dir = Tuplet_bracket::get_default_dir (me);
655   return scm_from_int (dir);
656 }
657
658 MAKE_SCHEME_CALLBACK (Tuplet_bracket, calc_positions, 1);
659 SCM
660 Tuplet_bracket::calc_positions (SCM smob)
661 {
662   Spanner *me = unsmob<Spanner> (smob);
663
664   Real dy = 0.0;
665   Real offset = 0.0;
666   calc_position_and_height (me, &offset, &dy);
667
668   SCM x = scm_cons (scm_from_double (offset),
669                     scm_from_double (offset + dy));
670
671   return x;
672 }
673
674 /*
675   similar to beam ?
676 */
677 Direction
678 Tuplet_bracket::get_default_dir (Grob *me)
679 {
680   Drul_array<int> dirs (0, 0);
681   extract_grob_set (me, "note-columns", columns);
682   for (vsize i = 0; i < columns.size (); i++)
683     {
684       Grob *nc = columns[i];
685       if (Note_column::has_rests (nc))
686         continue;
687       Direction d = Note_column::dir (nc);
688       if (d)
689         dirs[d]++;
690     }
691
692   if (dirs[UP] == dirs[DOWN])
693     {
694       if (dirs[UP] == 0)
695         return UP;
696       Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
697       if (!staff)
698         return UP;
699       Interval staff_extent = staff->extent (staff, Y_AXIS);
700       Interval extremal_positions;
701       extremal_positions.set_empty ();
702       for (vsize i = 0; i < columns.size (); i++)
703         {
704           Direction d = Note_column::dir (columns[i]);
705           extremal_positions[d] = minmax (d, 1.0 * Note_column::head_positions_interval (columns[i])[d], extremal_positions[d]);
706         }
707       for (LEFT_and_RIGHT (d))
708         extremal_positions[d] = -d * (staff_extent[d] - extremal_positions[d]);
709
710       return extremal_positions[UP] <= extremal_positions[DOWN] ? UP : DOWN;
711     }
712
713   return dirs[UP] > dirs[DOWN] ? UP : DOWN;
714 }
715
716 void
717 Tuplet_bracket::add_column (Grob *me, Item *n)
718 {
719   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
720   add_bound_item (dynamic_cast<Spanner *> (me), n);
721 }
722
723 void
724 Tuplet_bracket::add_script (Grob *me, Item *s)
725 {
726   Pointer_group_interface::add_grob (me, ly_symbol2scm ("scripts"), s);
727 }
728
729 void
730 Tuplet_bracket::add_tuplet_bracket (Grob *me, Grob *bracket)
731 {
732   Pointer_group_interface::add_grob (me, ly_symbol2scm ("tuplets"), bracket);
733 }
734
735 MAKE_SCHEME_CALLBACK (Tuplet_bracket, calc_cross_staff, 1);
736 SCM
737 Tuplet_bracket::calc_cross_staff (SCM smob)
738 {
739   Grob *me = unsmob<Grob> (smob);
740   extract_grob_set (me, "note-columns", cols);
741   extract_grob_set (me, "tuplets", tuplets);
742
743   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
744   commony = common_refpoint_of_array (tuplets, commony, Y_AXIS);
745   if (Grob *st = Staff_symbol_referencer::get_staff_symbol (me))
746     commony = st->common_refpoint (commony, Y_AXIS);
747   if (me->check_cross_staff (commony))
748     return SCM_BOOL_T;
749
750   bool equally_long = false;
751   Grob *par_beam = parallel_beam (me, cols, &equally_long);
752
753   if (par_beam && to_boolean (par_beam->get_property ("cross-staff")))
754     return SCM_BOOL_T;
755
756   for (vsize i = 0; i < cols.size (); i++)
757     {
758       Grob *stem = unsmob<Grob> (cols[i]->get_object ("stem"));
759       if (stem && to_boolean (stem->get_property ("cross-staff")))
760         return SCM_BOOL_T;
761     }
762
763   return SCM_BOOL_F;
764 }
765
766 ADD_INTERFACE (Tuplet_bracket,
767                "A bracket with a number in the middle, used for tuplets."
768                "  When the bracket spans a line break, the value of"
769                " @code{break-overshoot} determines how far it extends"
770                " beyond the staff.  At a line break, the markups in the"
771                " @code{edge-text} are printed at the edges.",
772
773                /* properties */
774                "avoid-scripts "
775                "bracket-flare "
776                "bracket-visibility "
777                "break-overshoot "
778                "connect-to-neighbor "
779                "direction "
780                "edge-height "
781                "edge-text "
782                "full-length-padding "
783                "full-length-to-extent "
784                "gap "
785                "positions "
786                "note-columns "
787                "padding "
788                "tuplet-number "
789                "scripts "
790                "shorten-pair "
791                "staff-padding "
792                "thickness "
793                "tuplets "
794                "X-positions "
795               );