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