2 tuplet-bracket.cc -- implement Tuplet_bracket
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 - tuplet bracket should probably be subject to the same rules as
14 beam sloping/quanting.
16 - There is no support for kneed brackets, or nested brackets.
18 - number placement for parallel beams should be much more advanced:
19 for sloped beams some extra horizontal offset must be introduced.
21 - number placement is usually done over the center note, not the
26 TODO: quantise, we don't want to collide with staff lines.
27 (or should we be above staff?)
29 todo: handle breaking elegantly.
34 #include "tuplet-bracket.hh"
35 #include "line-interface.hh"
38 #include "font-interface.hh"
39 #include "output-def.hh"
40 #include "text-item.hh"
42 #include "note-column.hh"
43 #include "group-interface.hh"
44 #include "directional-element-interface.hh"
46 #include "staff-symbol-referencer.hh"
50 get_x_bound_grob (Grob *g, Direction my_dir)
52 if (Note_column::get_stem (g)
53 && Note_column::dir (g) == my_dir)
55 g = Note_column::get_stem (g);
61 Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
66 Grob *s1 = Note_column::get_stem (cols[0]);
67 Grob *s2 = Note_column::get_stem (cols.top ());
69 Grob *b1 = s1 ? Stem::get_beam (s1) : 0;
70 Grob *b2 = s2 ? Stem::get_beam (s2) : 0;
72 Spanner *sp = dynamic_cast<Spanner *> (me);
74 *equally_long = false;
75 if (! (b1 && (b1 == b2) && !sp->is_broken ()))
78 Link_array<Grob> beam_stems = extract_grob_array (b1, ly_symbol2scm ("stems"));
79 if (beam_stems.size () == 0)
81 programming_error ("Beam under tuplet bracket has no stems!");
86 *equally_long = (beam_stems[0] == s1 && beam_stems.top () == s2);
93 in the case that there is no bracket, but there is a (single) beam,
94 follow beam precisely for determining tuplet number location.
96 MAKE_SCHEME_CALLBACK (Tuplet_bracket, print, 1);
98 Tuplet_bracket::print (SCM smob)
100 Grob *me = unsmob_grob (smob);
102 Link_array<Grob> columns
103 = extract_grob_array (me, ly_symbol2scm ("note-columns"));
105 if (!columns.size ())
106 return mol.smobbed_copy ();
109 SCM lp = me->get_property ("left-position");
110 SCM rp = me->get_property ("right-position");
112 if (!scm_is_number (rp) || !scm_is_number (lp))
113 after_line_breaking (smob);
116 Real ly = robust_scm2double (me->get_property ("left-position"), 0);
117 Real ry = robust_scm2double (me->get_property ("right-position"), 0);
119 bool equally_long = false;
120 Grob *par_beam = parallel_beam (me, columns, &equally_long);
122 Spanner *sp = dynamic_cast<Spanner *> (me);
124 bool bracket_visibility = !(par_beam && equally_long);
125 bool number_visibility = true;
128 Fixme: the type of this prop is sucky.
130 SCM bracket = me->get_property ("bracket-visibility");
131 if (scm_is_bool (bracket))
133 bracket_visibility = ly_scm2bool (bracket);
135 else if (bracket == ly_symbol2scm ("if-no-beam"))
136 bracket_visibility = !par_beam;
138 SCM numb = me->get_property ("number-visibility");
139 if (scm_is_bool (numb))
141 number_visibility = ly_scm2bool (numb);
143 else if (numb == ly_symbol2scm ("if-no-beam"))
144 number_visibility = !par_beam;
146 Grob *commonx = columns[0]->common_refpoint (columns.top (), X_AXIS);
149 Tuplet brackets are normally not broken, but we shouldn't crash if
152 commonx = commonx->common_refpoint (sp->get_bound (LEFT), X_AXIS);
153 commonx = commonx->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
155 Direction dir = get_grob_direction (me);
157 Grob *lgr = get_x_bound_grob (columns[0], dir);
158 Grob *rgr = get_x_bound_grob (columns.top (), dir);
160 Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
161 Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
164 SCM number = me->get_property ("text");
166 Output_def *pap = me->get_layout ();
168 if (scm_is_string (number) && number_visibility)
170 SCM properties = Font_interface::text_font_alist_chain (me);
171 SCM snum = Text_interface::interpret_markup (pap->self_scm (), properties, number);
172 num = *unsmob_stencil (snum);
173 num.align_to (X_AXIS, CENTER);
174 num.translate_axis (w / 2, X_AXIS);
175 num.align_to (Y_AXIS, CENTER);
177 num.translate_axis ((ry - ly) / 2, Y_AXIS);
179 mol.add_stencil (num);
183 No bracket when it would be smaller than the number.
185 TODO: should use GAP in calculation too.
187 if (bracket_visibility && number_visibility
188 && mol.extent (X_AXIS).length () > w)
190 bracket_visibility = false;
193 if (bracket_visibility)
195 Real ss = Staff_symbol_referencer::staff_space (me);
198 if (!num.extent (X_AXIS).is_empty ())
199 gap = num.extent (X_AXIS).length () + 1.0;
201 SCM fl = me->get_property ("bracket-flare");
202 SCM eh = me->get_property ("edge-height");
203 SCM sp = me->get_property ("shorten-pair");
206 Drul_array<Real> height, flare, shorten;
209 flare[d] = height[d] = shorten[d] = 0.0;
210 if (is_number_pair (fl))
211 flare[d] += ss * scm_to_double (index_get_cell (fl, d));
212 if (is_number_pair (eh))
213 height[d] += -dir * ss * scm_to_double (index_get_cell (eh, d));
214 if (is_number_pair (sp))
215 shorten[d] += ss * scm_to_double (index_get_cell (sp, d));
217 while (flip (&d) != LEFT);
219 Stencil brack = make_bracket (me, Y_AXIS,
223 0.1 = more space at right due to italics
224 TODO: use italic correction of font.
226 Interval (-0.5, 0.5) * gap + 0.1,
228 mol.add_stencil (brack);
231 mol.translate_axis (ly, Y_AXIS);
232 mol.translate_axis (x0 - sp->get_bound (LEFT)->relative_coordinate (commonx, X_AXIS), X_AXIS);
233 return mol.smobbed_copy ();
237 should move to lookup?
239 TODO: this will fail for very short (shorter than the flare)
243 Tuplet_bracket::make_bracket (Grob *me, // for line properties.
246 Drul_array<Real> height,
248 Drul_array<Real> flare,
249 Drul_array<Real> shorten)
251 Drul_array<Offset> corners (Offset (0, 0), dz);
253 Real length = dz.length ();
254 Drul_array<Offset> gap_corners;
256 Axis bracket_axis = other_axis (protusion_axis);
258 Drul_array<Offset> straight_corners = corners;
263 straight_corners[d] += -d * shorten[d] / length * dz;
265 while (flip (&d) != LEFT);
268 gap = Interval (0, 0);
271 gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
273 while (flip (&d) != LEFT);
275 Drul_array<Offset> flare_corners = straight_corners;
278 flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
279 flare_corners[d][protusion_axis] += height[d];
280 straight_corners[d][bracket_axis] += -d * flare[d];
282 while (flip (&d) != LEFT);
287 m.add_stencil (Line_interface::line (me, straight_corners[d],
290 m.add_stencil (Line_interface::line (me, straight_corners[d],
293 while (flip (&d) != LEFT);
299 use first -> last note for slope, and then correct for disturbing
302 Tuplet_bracket::calc_position_and_height (Grob *me, Real *offset, Real *dy)
304 Link_array<Grob> columns
305 = extract_grob_array (me, ly_symbol2scm ("note-columns"));
307 SCM cols = me->get_property ("note-columns");
308 Grob *commony = common_refpoint_of_list (cols, me, Y_AXIS);
309 Grob *commonx = common_refpoint_of_list (cols, me, X_AXIS);
312 if (Grob *st = Staff_symbol_referencer::get_staff_symbol (me))
313 staff = st->extent (commony, Y_AXIS);
315 Direction dir = get_grob_direction (me);
318 Use outer non-rest columns to determine slope
321 while (l < columns.size () && Note_column::has_rests (columns[l]))
324 int r = columns.size ()- 1;
325 while (r >= l && Note_column::has_rests (columns[r]))
330 Interval rv = columns[r]->extent (commony, Y_AXIS);
331 Interval lv = columns[l]->extent (commony, Y_AXIS);
334 Real graphical_dy = rv[dir] - lv[dir];
336 Slice ls = Note_column::head_positions_interval (columns[l]);
337 Slice rs = Note_column::head_positions_interval (columns[r]);
340 musical_dy[UP] = rs[UP] - ls[UP];
341 musical_dy[DOWN] = rs[DOWN] - ls[DOWN];
342 if (sign (musical_dy[UP]) != sign (musical_dy[DOWN]))
344 else if (sign (graphical_dy) != sign (musical_dy[DOWN]))
352 *offset = -dir * infinity_f;
354 if (!columns.size ())
357 Grob *lgr = get_x_bound_grob (columns[0], dir);
358 Grob *rgr = get_x_bound_grob (columns.top (), dir);
359 Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
360 Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
365 Real factor = columns.size () > 1 ? 1 / (x1 - x0) : 1.0;
367 for (int i = 0; i < columns.size (); i++)
369 Interval note_ext = columns[i]->extent (commony, Y_AXIS);
370 note_ext.unite (staff);
371 Real notey = note_ext[dir] - me->relative_coordinate (commony, Y_AXIS);
373 Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
374 Real tuplety = *dy * x * factor;
376 if (notey * dir > (*offset + tuplety) * dir)
377 *offset = notey - tuplety;
381 *offset += scm_to_double (me->get_property ("padding")) * dir;
384 horizontal brackets should not collide with staff lines.
387 Real ss = Staff_symbol_referencer::staff_space (me);
388 if (*dy == 0 && fabs (*offset) < ss * Staff_symbol_referencer::staff_radius (me))
390 // quantize, then do collision check.
393 *offset = rint (*offset);
394 if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
402 We depend on the beams if there are any.
404 MAKE_SCHEME_CALLBACK (Tuplet_bracket, before_line_breaking, 1);
406 Tuplet_bracket::before_line_breaking (SCM smob)
408 Grob *me = unsmob_grob (smob);
409 Link_array<Grob> columns
410 = extract_grob_array (me, ly_symbol2scm ("note-columns"));
412 for (int i = columns.size (); i--;)
414 Grob *s = Note_column::get_stem (columns[i]);
415 Grob *b = s ? Stem::get_beam (s) : 0;
417 me->add_dependency (b);
419 return SCM_UNSPECIFIED;
422 MAKE_SCHEME_CALLBACK (Tuplet_bracket, after_line_breaking, 1);
425 Tuplet_bracket::after_line_breaking (SCM smob)
427 Grob *me = unsmob_grob (smob);
428 Link_array<Grob> columns
429 = extract_grob_array (me, ly_symbol2scm ("note-columns"));
431 if (!columns.size ())
434 return SCM_UNSPECIFIED;
436 if (dynamic_cast<Spanner *> (me)->is_broken ())
438 me->warning (_ ("Killing tuplet bracket across linebreak."));
440 return SCM_UNSPECIFIED;
443 Direction dir = get_grob_direction (me);
446 dir = Tuplet_bracket::get_default_dir (me);
447 set_grob_direction (me, dir);
450 bool equally_long = false;
451 Grob *par_beam = parallel_beam (me, columns, &equally_long);
454 We follow the beam only if there is one, and we are next to it.
458 || get_grob_direction (par_beam) != dir)
460 calc_position_and_height (me, &offset, &dy);
464 SCM ps = par_beam->get_property ("positions");
466 Real lp = scm_to_double (scm_car (ps));
467 Real rp = scm_to_double (scm_cdr (ps));
472 offset = lp + dir * (0.5 + scm_to_double (me->get_property ("padding")));
476 SCM lp = me->get_property ("left-position");
477 SCM rp = me->get_property ("right-position");
479 if (scm_is_number (lp) && !scm_is_number (rp))
481 rp = scm_make_real (scm_to_double (lp) + dy);
483 else if (scm_is_number (rp) && !scm_is_number (lp))
485 lp = scm_make_real (scm_to_double (rp) - dy);
487 else if (!scm_is_number (rp) && !scm_is_number (lp))
489 lp = scm_make_real (offset);
490 rp = scm_make_real (offset + dy);
493 me->set_property ("left-position", lp);
494 me->set_property ("right-position", rp);
496 return SCM_UNSPECIFIED;
503 Tuplet_bracket::get_default_dir (Grob *me)
505 Drul_array<int> dirs (0, 0);
506 for (SCM s = me->get_property ("note-columns"); scm_is_pair (s); s = scm_cdr (s))
508 Grob *nc = unsmob_grob (scm_car (s));
509 Direction d = Note_column::dir (nc);
514 return dirs[UP] >= dirs[DOWN] ? UP : DOWN;
518 Tuplet_bracket::add_column (Grob *me, Item *n)
520 Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
521 me->add_dependency (n);
523 add_bound_item (dynamic_cast<Spanner *> (me), n);
526 ADD_INTERFACE (Tuplet_bracket,
527 "tuplet-bracket-interface",
528 "A bracket with a number in the middle, used for tuplets.",
530 "note-columns bracket-flare edge-height shorten-pair "
531 "padding left-position right-position bracket-visibility "
532 "number-visibility thickness direction");