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