2 tuplet-bracket.cc -- implement Tuplet_bracket
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 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
28 #include "line-interface.hh"
32 #include "font-interface.hh"
34 #include "output-def.hh"
35 #include "text-item.hh"
36 #include "tuplet-bracket.hh"
38 #include "note-column.hh"
39 #include "group-interface.hh"
40 #include "directional-element-interface.hh"
42 #include "staff-symbol-referencer.hh"
47 get_x_bound_grob (Grob *g, Direction my_dir)
49 if (Note_column::get_stem (g)
50 && Note_column::dir (g) == my_dir)
52 g = Note_column::get_stem (g);
60 Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
65 Grob *s1 = Note_column::get_stem (cols[0]);
66 Grob *s2 = Note_column::get_stem (cols.top ());
68 Grob*b1 = s1 ? Stem::get_beam (s1) : 0;
69 Grob*b2 = s2 ? Stem::get_beam (s2) : 0;
71 Spanner*sp = dynamic_cast<Spanner*> (me);
74 if (! (b1 && (b1 == b2) && !sp->is_broken ()))
77 Link_array<Grob> beam_stems = Pointer_group_interface__extract_grobs
78 (b1, (Grob*)0, "stems");
80 if (beam_stems.size () == 0)
82 programming_error ("Beam under tuplet bracket has no stems!");
87 *equally_long = (beam_stems[0] == s1 && beam_stems.top () == s2);
95 in the case that there is no bracket, but there is a (single) beam,
96 follow beam precisely for determining tuplet number location.
99 MAKE_SCHEME_CALLBACK (Tuplet_bracket,print,1);
101 Tuplet_bracket::print (SCM smob)
103 Grob *me= unsmob_grob (smob);
105 Link_array<Grob> columns=
106 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
108 if (!columns.size ())
109 return mol.smobbed_copy ();
114 SCM lp = me->get_property ("left-position");
115 SCM rp = me->get_property ("right-position");
117 if (!ly_c_number_p (rp) || !ly_c_number_p (lp))
118 after_line_breaking (smob);
121 Real ly = robust_scm2double (me->get_property ("left-position"), 0);
122 Real ry = robust_scm2double (me->get_property ("right-position"), 0);
124 bool equally_long = false;
125 Grob * par_beam = parallel_beam (me, columns, &equally_long);
127 Spanner*sp = dynamic_cast<Spanner*> (me);
129 bool bracket_visibility = !(par_beam && equally_long);
130 bool number_visibility = true;
133 Fixme: the type of this prop is sucky.
135 SCM bracket = me->get_property ("bracket-visibility");
136 if (ly_c_boolean_p (bracket))
138 bracket_visibility = ly_scm2bool (bracket);
140 else if (bracket == ly_symbol2scm ("if-no-beam"))
141 bracket_visibility = !par_beam;
143 SCM numb = me->get_property ("number-visibility");
144 if (ly_c_boolean_p (numb))
146 number_visibility = ly_scm2bool (numb);
148 else if (numb == ly_symbol2scm ("if-no-beam"))
149 number_visibility = !par_beam;
151 Grob * commonx = columns[0]->common_refpoint (columns.top (),X_AXIS);
154 Tuplet brackets are normally not broken, but we shouldn't crash if
157 commonx = commonx->common_refpoint (sp->get_bound (LEFT), X_AXIS);
158 commonx = commonx->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
160 Direction dir = get_grob_direction (me);
162 Grob * lgr = get_x_bound_grob (columns[0], dir);
163 Grob * rgr = get_x_bound_grob (columns.top (), dir);
164 Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
165 Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
169 SCM number = me->get_property ("text");
171 Output_def *pap = me->get_paper ();
173 if (ly_c_string_p (number) && number_visibility)
175 SCM properties = Font_interface::text_font_alist_chain (me);
176 SCM snum = Text_item::interpret_markup (pap->self_scm (), properties, number);
177 num = *unsmob_stencil (snum);
178 num.align_to (X_AXIS, CENTER);
179 num.translate_axis (w/2, X_AXIS);
180 num.align_to (Y_AXIS, CENTER);
182 num.translate_axis ((ry-ly)/2, Y_AXIS);
184 mol.add_stencil (num);
189 No bracket when it would be smaller than the number.
191 TODO: should use GAP in calculation too.
193 if (bracket_visibility && number_visibility
194 && mol.extent (X_AXIS).length () > w)
196 bracket_visibility = false;
199 if (bracket_visibility)
201 Real ss = Staff_symbol_referencer::staff_space (me);
204 if (!num.extent (X_AXIS).is_empty ())
205 gap = num.extent (X_AXIS).length () + 1.0;
207 SCM fl = me->get_property ("bracket-flare");
208 SCM eh = me->get_property ("edge-height");
209 SCM sp = me->get_property ("shorten-pair");
212 Drul_array<Real> height, flare, shorten;
214 flare[d] = height[d] = shorten[d] = 0.0;
215 if (is_number_pair (fl))
216 flare[d] += ss * ly_scm2double (index_get_cell (fl, d));
217 if (is_number_pair (eh))
218 height[d] += - dir * ss *ly_scm2double (index_get_cell (eh, d));
219 if (is_number_pair (sp))
220 shorten[d] += ss *ly_scm2double (index_get_cell (sp, d));
222 while (flip (&d) != LEFT);
224 Stencil brack = make_bracket (me, Y_AXIS,
228 0.1 = more space at right due to italics
229 TODO: use italic correction of font.
231 Interval (-0.5, 0.5) * gap + 0.1,
233 mol.add_stencil (brack);
236 mol.translate_axis (ly, Y_AXIS);
237 mol.translate_axis (x0 - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
238 return mol.smobbed_copy ();
242 should move to lookup?
244 TODO: this will fail for very short (shorter than the flare)
248 Tuplet_bracket::make_bracket (Grob *me, // for line properties.
251 Drul_array<Real> height,
253 Drul_array<Real> flare,
254 Drul_array<Real> shorten)
256 Drul_array<Offset> corners (Offset (0,0), dz);
258 Real length = dz.length ();
259 Drul_array<Offset> gap_corners;
262 Axis bracket_axis = other_axis (protusion_axis);
264 Drul_array<Offset> straight_corners = corners;
268 straight_corners[d] += - d * shorten[d] /length * dz;
269 } while (flip (&d) != LEFT);
273 gap = Interval (0,0);
275 gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
276 } while (flip (&d) != LEFT);
278 Drul_array<Offset> flare_corners = straight_corners;
280 flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
281 flare_corners[d][protusion_axis] += height[d];
282 straight_corners[d][bracket_axis] += - d * flare[d];
283 } 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],
292 } 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 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
308 SCM cols = me->get_property ("note-columns");
309 Grob * commony = common_refpoint_of_list (cols, me, Y_AXIS);
310 Grob * commonx = common_refpoint_of_list (cols, me, X_AXIS);
313 if (Grob * st = Staff_symbol_referencer::get_staff_symbol (me))
314 staff = st->extent (commony, Y_AXIS);
316 Direction dir = get_grob_direction (me);
319 Use outer non-rest columns to determine slope
322 while (l <columns.size () && Note_column::has_rests (columns[l]))
325 int r = columns.size ()- 1;
326 while (r >= l && Note_column::has_rests (columns[r]))
331 Interval rv =columns[r]->extent (commony, Y_AXIS);
332 Interval lv =columns[l]->extent (commony, Y_AXIS);
335 Real graphical_dy = rv[dir] - lv[dir];
337 Slice ls = Note_column::head_positions_interval (columns[l]);
338 Slice rs = Note_column::head_positions_interval (columns[r]);
341 musical_dy[UP] = rs[UP] - ls[UP];
342 musical_dy[DOWN] = rs[DOWN] - ls[DOWN];
343 if (sign (musical_dy[UP]) != sign (musical_dy[DOWN]))
345 else if (sign (graphical_dy) != sign (musical_dy[DOWN]))
354 *offset = - dir * infinity_f;
356 if (!columns.size ())
361 Grob * lgr = get_x_bound_grob (columns[0], dir);
362 Grob * rgr = get_x_bound_grob (columns.top (), dir);
363 Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
364 Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
370 Real factor = columns.size () > 1 ? 1/ (x1 - x0) : 1.0;
372 for (int i = 0; i < columns.size (); i++)
374 Interval note_ext =columns[i]->extent (commony, Y_AXIS);
375 note_ext.unite (staff);
376 Real notey = note_ext[dir] - me->relative_coordinate (commony, Y_AXIS);
378 Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
379 Real tuplety = *dy * x * factor;
381 if (notey * dir > (*offset + tuplety) * dir)
382 *offset = notey - tuplety;
386 *offset += ly_scm2double (me->get_property ("padding")) *dir;
390 horizontal brackets should not collide with staff lines.
393 Real ss= Staff_symbol_referencer::staff_space (me);
394 if (*dy == 0 && fabs (*offset) < ss * Staff_symbol_referencer::staff_radius (me))
396 // quantize, then do collision check.
399 *offset = rint (*offset);
400 if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
411 We depend on the beams if there are any.
413 MAKE_SCHEME_CALLBACK (Tuplet_bracket,before_line_breaking,1);
415 Tuplet_bracket::before_line_breaking (SCM smob)
417 Grob *me = unsmob_grob (smob);
418 Link_array<Grob> columns=
419 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
422 for (int i = columns.size (); i--;)
424 Grob * s =Note_column::get_stem (columns[i]);
425 Grob * b = s ? Stem::get_beam (s): 0;
427 me->add_dependency (b);
429 return SCM_UNSPECIFIED;
432 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
435 Tuplet_bracket::after_line_breaking (SCM smob)
437 Grob * me = unsmob_grob (smob);
438 Link_array<Grob> columns=
439 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
441 if (!columns.size ())
444 return SCM_UNSPECIFIED;
446 if (dynamic_cast<Spanner*> (me)->is_broken ())
448 me->warning (_("Killing tuplet bracket across linebreak."));
450 return SCM_UNSPECIFIED;
453 Direction dir = get_grob_direction (me);
456 dir = Tuplet_bracket::get_default_dir (me);
457 set_grob_direction (me, dir);
460 bool equally_long = false;
461 Grob * par_beam = parallel_beam (me, columns, &equally_long);
464 We follow the beam only if there is one, and we are next to it.
468 || get_grob_direction (par_beam) != dir)
470 calc_position_and_height (me,&offset,&dy);
474 SCM ps = par_beam->get_property ("positions");
476 Real lp = ly_scm2double (ly_car (ps));
477 Real rp = ly_scm2double (ly_cdr (ps));
482 offset = lp + dir * (0.5 + ly_scm2double (me->get_property ("padding")));
487 SCM lp = me->get_property ("left-position");
488 SCM rp = me->get_property ("right-position");
490 if (ly_c_number_p (lp) && !ly_c_number_p (rp))
492 rp = scm_make_real (ly_scm2double (lp) + dy);
494 else if (ly_c_number_p (rp) && !ly_c_number_p (lp))
496 lp = scm_make_real (ly_scm2double (rp) - dy);
498 else if (!ly_c_number_p (rp) && !ly_c_number_p (lp))
500 lp = scm_make_real (offset);
501 rp = scm_make_real (offset +dy);
504 me->set_property ("left-position", lp);
505 me->set_property ("right-position", rp);
507 return SCM_UNSPECIFIED;
515 Tuplet_bracket::get_default_dir (Grob*me)
517 Drul_array<int> dirs (0,0);
518 for (SCM s = me->get_property ("note-columns"); ly_c_pair_p (s); s = ly_cdr (s))
520 Grob * nc = unsmob_grob (ly_car (s));
521 Direction d = Note_column::dir (nc);
526 return dirs[UP] >= dirs[DOWN] ? UP : DOWN;
530 Tuplet_bracket::add_column (Grob*me, Item*n)
532 Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
533 me->add_dependency (n);
535 add_bound_item (dynamic_cast<Spanner*> (me), n);
543 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
544 "A bracket with a number in the middle, used for tuplets.",
545 "note-columns bracket-flare edge-height shorten-pair padding left-position right-position bracket-visibility number-visibility thickness direction");