2 plet-spanner.cc -- implement Tuplet_bracket
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Jan Nieuwenhuizen <janneke@gnu.org>
12 - tuplet bracket should probably be subject to the same rules as
13 beam sloping/quanting.
15 - There is no support for kneed brackets, or nested brackets.
17 - number placement for parallel beams should be much more advanced:
18 for sloped beams some extra horizontal offset must be introduced.
20 - number placement is usually done over the center note, not the
30 #include "font-interface.hh"
31 #include "molecule.hh"
32 #include "paper-def.hh"
33 #include "text-item.hh"
34 #include "tuplet-bracket.hh"
36 #include "note-column.hh"
37 #include "group-interface.hh"
38 #include "directional-element-interface.hh"
40 #include "staff-symbol-referencer.hh"
45 get_x_bound_grob (Grob *g, Direction my_dir)
47 if (Note_column::stem_l (g)
48 && Note_column::dir (g) == my_dir)
50 g = Note_column::stem_l (g);
58 Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
63 Grob *s1 = Note_column::stem_l (cols[0]);
64 Grob *s2 = Note_column::stem_l (cols.top());
66 Grob*b1 = s1 ? Stem::beam_l (s1) : 0;
67 Grob*b2 = s2 ? Stem::beam_l (s2) : 0;
69 Spanner*sp = dynamic_cast<Spanner*> (me);
72 if (! ( b1 && (b1 == b2) && !sp->broken_b() ))
75 Link_array<Grob> beam_stems = Pointer_group_interface__extract_grobs
76 (b1, (Grob*)0, "stems");
79 *equally_long = (beam_stems[0] == s1 && beam_stems.top() == s2);
87 in the case that there is no bracket, but there is a (single) beam,
88 follow beam precisely for determining tuplet number location.
91 MAKE_SCHEME_CALLBACK (Tuplet_bracket,brew_molecule,1);
93 Tuplet_bracket::brew_molecule (SCM smob)
95 Grob *me= unsmob_grob (smob);
97 Link_array<Grob> column_arr=
98 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
100 if (!column_arr.size ())
101 return mol.smobbed_copy ();
103 bool equally_long = false;
104 Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
106 Spanner*sp = dynamic_cast<Spanner*> (me);
108 bool bracket_visibility = !(par_beam && equally_long);
109 bool number_visibility = true;
112 Fixme: the type of this prop is sucky.
114 SCM bracket = me->get_grob_property ("bracket-visibility");
115 if (gh_boolean_p (bracket))
117 bracket_visibility = gh_scm2bool (bracket);
119 else if (bracket == ly_symbol2scm ("if-no-beam"))
120 bracket_visibility = !par_beam;
122 SCM numb = me->get_grob_property ("number-visibility");
123 if (gh_boolean_p (numb))
125 number_visibility = gh_scm2bool (numb);
127 else if (numb == ly_symbol2scm ("if-no-beam"))
128 number_visibility = !par_beam;
130 Grob * commonx = column_arr[0]->common_refpoint (column_arr.top (),X_AXIS);
131 Direction dir = Directional_element_interface::get (me);
133 Grob * lgr = get_x_bound_grob (column_arr[0], dir);
134 Grob * rgr = get_x_bound_grob (column_arr.top(), dir);
135 Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
136 Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
140 Real ly = gh_scm2double (me->get_grob_property ("left-position"));
141 Real ry = gh_scm2double (me->get_grob_property ("right-position"));
142 SCM number = me->get_grob_property ("text");
144 if (gh_string_p (number) && number_visibility)
146 SCM properties = Font_interface::font_alist_chain (me);
147 Molecule num = Text_item::text2molecule (me, number, properties);
148 num.align_to (X_AXIS, CENTER);
149 num.translate_axis (w/2, X_AXIS);
150 num.align_to (Y_AXIS, CENTER);
152 num.translate_axis ((ry-ly)/2, Y_AXIS);
154 mol.add_molecule (num);
157 if (bracket_visibility)
159 Real lt = me->paper_l ()->get_var ("linethickness");
161 SCM thick = me->get_grob_property ("thickness");
162 if (gh_number_p (thick))
163 lt *= gh_scm2double (thick);
165 SCM gap = me->get_grob_property ("gap");
166 SCM ew = me->get_grob_property ("edge-width");
167 SCM eh = me->get_grob_property ("edge-height");
168 SCM sp = me->get_grob_property ("shorten-pair");
171 Drul_array<Real> height, width, shorten;
173 width[d] = height[d] = shorten[d] = 0.0;
174 if ( gh_pair_p (ew) )
175 width[d] += gh_scm2double (index_cell (ew, d)) * d;
176 if ( gh_pair_p (eh) )
177 height[d] += gh_scm2double (index_cell (eh, d));
178 if ( gh_pair_p (sp) )
179 shorten[d] += gh_scm2double (index_cell (sp, d));
181 while (flip (&d) != LEFT);
183 Molecule brack = make_bracket (Y_AXIS,
185 -height[LEFT]*dir, -height[RIGHT]*dir,
187 width[LEFT], width[RIGHT],
188 shorten[LEFT], shorten[RIGHT]);
189 mol.add_molecule (brack);
190 mol.translate_axis (dir * max(height[LEFT], height[RIGHT]), Y_AXIS);
193 mol.translate_axis (ly, Y_AXIS);
194 mol.translate_axis (x0 - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
195 return mol.smobbed_copy ();
199 should move to lookup?
202 Tuplet_bracket::make_bracket (Axis protusion_axis,
203 Real dx, Real dy, Real thick, Real left_height,
204 Real right_height, Real gap, Real left_widen,
205 Real right_widen, Real left_shorten, Real right_shorten)
207 Real len = Offset (dx,dy).length ();
208 Real gapx = dx * (gap / len);
209 Real gapy = dy * (gap / len);
210 Real lshortx = dx * (left_shorten / len);
211 Real lshorty = dy * (left_shorten / len);
212 Real rshortx = dx * (right_shorten / len);
213 Real rshorty = dy * (right_shorten / len);
214 Axis other = other_axis (protusion_axis);
216 Molecule l1 = Lookup::line (thick, Offset(lshortx, lshorty),
217 Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
219 Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
220 Offset (dx - rshortx, dy - rshorty));
223 protusion[other] = -left_widen;
224 protusion[protusion_axis] = left_height;
225 Molecule p1 = Lookup::line (thick,
226 Offset(lshortx, lshorty),
227 Offset(lshortx, lshorty) + protusion);
228 protusion[other] = right_widen;
229 protusion[protusion_axis] = right_height;
230 Molecule p2 = Lookup::line (thick,
231 Offset(dx - rshortx, dy - rshorty),
232 Offset(dx - rshortx, dy - rshorty) + protusion);
245 use first -> last note for slope, and then correct for disturbing
248 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
250 Link_array<Grob> column_arr=
251 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
254 SCM cols = me->get_grob_property ("note-columns");
255 Grob * commony = common_refpoint_of_list (cols, me, Y_AXIS);
256 Grob * commonx = common_refpoint_of_list (cols, me, X_AXIS);
258 Direction dir = Directional_element_interface::get (me);
261 Use outer non-rest columns to determine slope
264 while (l <column_arr.size () && Note_column::rest_b (column_arr[l]))
267 int r = column_arr.size ()- 1;
268 while (r >= l && Note_column::rest_b (column_arr[r]))
273 *dy = column_arr[r]->extent (commony, Y_AXIS) [dir]
274 - column_arr[l]->extent (commony, Y_AXIS) [dir] ;
280 *offset = - dir * infinity_f;
282 if (!column_arr.size ())
287 Grob * lgr = get_x_bound_grob (column_arr[0], dir);
288 Grob * rgr = get_x_bound_grob (column_arr.top(), dir);
289 Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
290 Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
296 Real factor = column_arr.size () > 1 ? 1/ (x1 - x0) : 1.0;
298 for (int i = 0; i < column_arr.size (); i++)
300 Real notey = column_arr[i]->extent (commony, Y_AXIS)[dir]
301 - me->relative_coordinate (commony, Y_AXIS);
303 Real x = column_arr[i]->relative_coordinate (commonx, X_AXIS) - x0;
304 Real tuplety = *dy * x * factor;
306 if (notey * dir > (*offset + tuplety) * dir)
307 *offset = notey - tuplety;
311 *offset += gh_scm2double (me->get_grob_property ("padding")) *dir;
315 horizontal brackets should not collide with staff lines.
319 // quantize, then do collision check.
320 Real ss= Staff_symbol_referencer::staff_space (me);
323 *offset = rint (*offset);
324 if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
333 use first -> last note for slope,
336 Tuplet_bracket::calc_dy (Grob*me,Real * dy)
338 Link_array<Grob> column_arr=
339 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
344 Direction d = Directional_element_interface::get (me);
345 *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
346 - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
351 We depend on the beams if there are any.
353 MAKE_SCHEME_CALLBACK (Tuplet_bracket,before_line_breaking,1);
355 Tuplet_bracket::before_line_breaking (SCM smob)
357 Grob *me = unsmob_grob (smob);
358 Link_array<Grob> column_arr=
359 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
362 for (int i = column_arr.size(); i--;)
364 Grob * s =Note_column::stem_l (column_arr[i]);
365 Grob * b = s ? Stem::beam_l (s): 0;
367 me->add_dependency (b);
369 return SCM_UNDEFINED;
372 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
375 Tuplet_bracket::after_line_breaking (SCM smob)
377 Grob * me = unsmob_grob (smob);
378 Link_array<Grob> column_arr=
379 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
381 if (!column_arr.size ())
384 return SCM_UNSPECIFIED;
386 if (dynamic_cast<Spanner*> (me)->broken_b ())
388 me->warning ( "Tuplet_bracket was across linebreak. Farewell cruel world.");
390 return SCM_UNSPECIFIED;
393 Direction dir = Directional_element_interface::get (me);
396 dir = Tuplet_bracket::get_default_dir (me);
397 Directional_element_interface::set (me, dir);
400 bool equally_long = false;
401 Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
404 We follow the beam only if there is one, and we are next to it.
408 || Directional_element_interface::get (par_beam) != dir)
410 calc_position_and_height (me,&offset,&dy);
414 SCM ps = par_beam->get_grob_property ("positions");
416 Real lp = gh_scm2double (gh_car (ps));
417 Real rp = gh_scm2double (gh_cdr (ps));
422 offset = lp + dir * (0.5 + gh_scm2double (me->get_grob_property ("padding")));
427 SCM lp = me->get_grob_property ("left-position");
428 SCM rp = me->get_grob_property ("right-position");
430 if (gh_number_p (lp) && !gh_number_p (rp))
432 rp = gh_double2scm (gh_scm2double (lp) + dy);
434 else if (gh_number_p (rp) && !gh_number_p (lp))
436 lp = gh_double2scm (gh_scm2double (rp) - dy);
438 else if (!gh_number_p (rp) && !gh_number_p (lp))
440 lp = gh_double2scm (offset);
441 rp = gh_double2scm (offset +dy);
444 me->set_grob_property ("left-position", lp);
445 me->set_grob_property ("right-position", rp);
447 return SCM_UNSPECIFIED;
455 Tuplet_bracket::get_default_dir (Grob*me)
458 for (SCM s = me->get_grob_property ("note-columns"); gh_pair_p (s); s = ly_cdr (s))
460 Grob * nc = unsmob_grob (ly_car (s));
461 if (Note_column::dir (nc) < 0)
471 Tuplet_bracket::add_column (Grob*me, Item*n)
473 Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
474 me->add_dependency (n);
476 add_bound_item (dynamic_cast<Spanner*> (me), n);
484 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
485 "A bracket with a number in the middle, used for tuplets.",
486 "note-columns edge-width edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");