2 plet-spanner.cc -- implement Tuplet_bracket
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 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::get_stem (g
)
48 && Note_column::dir (g
) == my_dir
)
50 g
= Note_column::get_stem (g
);
58 Tuplet_bracket::parallel_beam (Grob
*me
, Link_array
<Grob
> const &cols
, bool *equally_long
)
63 Grob
*s1
= Note_column::get_stem (cols
[0]);
64 Grob
*s2
= Note_column::get_stem (cols
.top());
66 Grob
*b1
= s1
? Stem::get_beam (s1
) : 0;
67 Grob
*b2
= s2
? Stem::get_beam (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");
78 if (beam_stems
.size() == 0)
80 programming_error ("Beam under tuplet bracket has no stems!");
85 *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.
97 MAKE_SCHEME_CALLBACK (Tuplet_bracket
,brew_molecule
,1);
99 Tuplet_bracket::brew_molecule (SCM smob
)
101 Grob
*me
= unsmob_grob (smob
);
103 Link_array
<Grob
> columns
=
104 Pointer_group_interface__extract_grobs (me
, (Grob
*)0, "note-columns");
106 if (!columns
.size ())
107 return mol
.smobbed_copy ();
112 SCM lp
= me
->get_grob_property ("left-position");
113 SCM rp
= me
->get_grob_property ("right-position");
115 if (!gh_number_p (rp
) || !gh_number_p (lp
))
116 after_line_breaking (smob
);
119 Real ly
= gh_scm2double (me
->get_grob_property ("left-position"));
120 Real ry
= gh_scm2double (me
->get_grob_property ("right-position"));
122 bool equally_long
= false;
123 Grob
* par_beam
= parallel_beam (me
, columns
, &equally_long
);
125 Spanner
*sp
= dynamic_cast<Spanner
*> (me
);
127 bool bracket_visibility
= !(par_beam
&& equally_long
);
128 bool number_visibility
= true;
131 Fixme: the type of this prop is sucky.
133 SCM bracket
= me
->get_grob_property ("bracket-visibility");
134 if (gh_boolean_p (bracket
))
136 bracket_visibility
= gh_scm2bool (bracket
);
138 else if (bracket
== ly_symbol2scm ("if-no-beam"))
139 bracket_visibility
= !par_beam
;
141 SCM numb
= me
->get_grob_property ("number-visibility");
142 if (gh_boolean_p (numb
))
144 number_visibility
= gh_scm2bool (numb
);
146 else if (numb
== ly_symbol2scm ("if-no-beam"))
147 number_visibility
= !par_beam
;
149 Grob
* commonx
= columns
[0]->common_refpoint (columns
.top (),X_AXIS
);
152 Tuplet brackets are normally not broken, but we shouldn't crash if
155 commonx
= commonx
->common_refpoint (sp
->get_bound(LEFT
), X_AXIS
);
156 commonx
= commonx
->common_refpoint (sp
->get_bound(RIGHT
), X_AXIS
);
158 Direction dir
= get_grob_direction (me
);
160 Grob
* lgr
= get_x_bound_grob (columns
[0], dir
);
161 Grob
* rgr
= get_x_bound_grob (columns
.top(), dir
);
162 Real x0
= lgr
->extent (commonx
,X_AXIS
)[LEFT
];
163 Real x1
= rgr
->extent (commonx
,X_AXIS
)[RIGHT
];
167 SCM number
= me
->get_grob_property ("text");
169 Paper_def
*pap
= me
->get_paper ();
170 if (gh_string_p (number
) && number_visibility
)
172 SCM properties
= Font_interface::font_alist_chain (me
);
173 SCM snum
= Text_item::interpret_markup (pap
->self_scm (), properties
, number
);
174 Molecule num
= *unsmob_molecule (snum
);
175 num
.align_to (X_AXIS
, CENTER
);
176 num
.translate_axis (w
/2, X_AXIS
);
177 num
.align_to (Y_AXIS
, CENTER
);
179 num
.translate_axis ((ry
-ly
)/2, Y_AXIS
);
181 mol
.add_molecule (num
);
186 No bracket when it would be smaller than the number.
188 TODO: should use GAP in calculation too.
190 if (bracket_visibility
&& number_visibility
191 && mol
.extent (X_AXIS
).length () > w
)
193 bracket_visibility
= false;
196 if (bracket_visibility
)
198 Real lt
= pap
->get_realvar (ly_symbol2scm ("linethickness"));
200 SCM thick
= me
->get_grob_property ("thickness");
201 if (gh_number_p (thick
))
202 lt
*= gh_scm2double (thick
);
204 SCM gap
= me
->get_grob_property ("gap");
206 SCM fl
= me
->get_grob_property ("bracket-flare");
207 SCM eh
= me
->get_grob_property ("edge-height");
208 SCM sp
= me
->get_grob_property ("shorten-pair");
211 Drul_array
<Real
> height
, flare
, shorten
;
213 flare
[d
] = height
[d
] = shorten
[d
] = 0.0;
214 if (is_number_pair (fl
))
215 flare
[d
] += gh_scm2double (index_get_cell (fl
, d
));
216 if (is_number_pair (eh
))
217 height
[d
] += gh_scm2double (index_get_cell (eh
, d
)) * - dir
;
218 if (is_number_pair (sp
))
219 shorten
[d
] += gh_scm2double (index_get_cell (sp
, d
));
221 while (flip (&d
) != LEFT
);
223 Molecule brack
= make_bracket (Y_AXIS
,
229 mol
.add_molecule (brack
);
232 mol
.translate_axis (ly
, Y_AXIS
);
233 mol
.translate_axis (x0
- sp
->get_bound (LEFT
)->relative_coordinate (commonx
,X_AXIS
),X_AXIS
);
234 return mol
.smobbed_copy ();
238 should move to lookup?
240 TODO: this will fail for very short (shorter than the flare)
244 Tuplet_bracket::make_bracket (Axis protusion_axis
,
245 Real dx
, Real dy
, Real thick
, Drul_array
<Real
> height
,
247 Drul_array
<Real
> flare
,
248 Drul_array
<Real
> shorten
)
250 Offset dz
= Offset (dx
,dy
);
251 Drul_array
<Offset
> corners (Offset(0,0), dz
);
253 Real length
= dz
.length ();
254 Drul_array
<Offset
> gap_corners
;
257 Axis bracket_axis
= other_axis (protusion_axis
);
259 Drul_array
<Offset
> straight_corners
= corners
;
263 straight_corners
[d
] += - d
* shorten
[d
] /length
* dz
;
264 gap_corners
[d
] = (dz
* 0.5) + d
* gap
/ length
* dz
;
265 } while (flip (&d
) != LEFT
);
267 Drul_array
<Offset
> flare_corners
= straight_corners
;
269 flare_corners
[d
][bracket_axis
] = straight_corners
[d
][bracket_axis
];
270 flare_corners
[d
][protusion_axis
] += height
[d
];
271 straight_corners
[d
][bracket_axis
] += - d
* flare
[d
];
272 } while (flip (&d
) != LEFT
);
277 m
.add_molecule (Lookup::line (thick
, straight_corners
[d
],
280 m
.add_molecule (Lookup::line (thick
, straight_corners
[d
],
282 } while (flip (&d
) != LEFT
);
289 use first -> last note for slope, and then correct for disturbing
292 Tuplet_bracket::calc_position_and_height (Grob
*me
,Real
*offset
, Real
* dy
)
294 Link_array
<Grob
> columns
=
295 Pointer_group_interface__extract_grobs (me
, (Grob
*)0, "note-columns");
298 SCM cols
= me
->get_grob_property ("note-columns");
299 Grob
* commony
= common_refpoint_of_list (cols
, me
, Y_AXIS
);
300 Grob
* commonx
= common_refpoint_of_list (cols
, me
, X_AXIS
);
302 Direction dir
= get_grob_direction (me
);
305 Use outer non-rest columns to determine slope
308 while (l
<columns
.size () && Note_column::rest_b (columns
[l
]))
311 int r
= columns
.size ()- 1;
312 while (r
>= l
&& Note_column::rest_b (columns
[r
]))
317 *dy
= columns
[r
]->extent (commony
, Y_AXIS
) [dir
]
318 - columns
[l
]->extent (commony
, Y_AXIS
) [dir
] ;
324 *offset
= - dir
* infinity_f
;
326 if (!columns
.size ())
331 Grob
* lgr
= get_x_bound_grob (columns
[0], dir
);
332 Grob
* rgr
= get_x_bound_grob (columns
.top(), dir
);
333 Real x0
= lgr
->extent (commonx
,X_AXIS
)[LEFT
];
334 Real x1
= rgr
->extent (commonx
,X_AXIS
)[RIGHT
];
340 Real factor
= columns
.size () > 1 ? 1/ (x1
- x0
) : 1.0;
342 for (int i
= 0; i
< columns
.size (); i
++)
344 Real notey
= columns
[i
]->extent (commony
, Y_AXIS
)[dir
]
345 - me
->relative_coordinate (commony
, Y_AXIS
);
347 Real x
= columns
[i
]->relative_coordinate (commonx
, X_AXIS
) - x0
;
348 Real tuplety
= *dy
* x
* factor
;
350 if (notey
* dir
> (*offset
+ tuplety
) * dir
)
351 *offset
= notey
- tuplety
;
355 *offset
+= gh_scm2double (me
->get_grob_property ("padding")) *dir
;
359 horizontal brackets should not collide with staff lines.
362 Real ss
= Staff_symbol_referencer::staff_space (me
);
363 if (*dy
== 0 && fabs (*offset
) < ss
* Staff_symbol_referencer::staff_radius (me
))
365 // quantize, then do collision check.
368 *offset
= rint (*offset
);
369 if (Staff_symbol_referencer::on_staffline (me
, (int) rint (*offset
)))
378 use first -> last note for slope,
381 Tuplet_bracket::calc_dy (Grob
*me
,Real
* dy
)
383 Link_array
<Grob
> columns
=
384 Pointer_group_interface__extract_grobs (me
, (Grob
*)0, "note-columns");
389 Direction d
= get_grob_direction (me
);
390 *dy
= columns
.top ()->extent (columns
.top (), Y_AXIS
) [d
]
391 - columns
[0]->extent (columns
[0], Y_AXIS
) [d
];
396 We depend on the beams if there are any.
398 MAKE_SCHEME_CALLBACK (Tuplet_bracket
,before_line_breaking
,1);
400 Tuplet_bracket::before_line_breaking (SCM smob
)
402 Grob
*me
= unsmob_grob (smob
);
403 Link_array
<Grob
> columns
=
404 Pointer_group_interface__extract_grobs (me
, (Grob
*)0, "note-columns");
407 for (int i
= columns
.size(); i
--;)
409 Grob
* s
=Note_column::get_stem (columns
[i
]);
410 Grob
* b
= s
? Stem::get_beam (s
): 0;
412 me
->add_dependency (b
);
414 return SCM_UNDEFINED
;
417 MAKE_SCHEME_CALLBACK (Tuplet_bracket
,after_line_breaking
,1);
420 Tuplet_bracket::after_line_breaking (SCM smob
)
422 Grob
* me
= unsmob_grob (smob
);
423 Link_array
<Grob
> columns
=
424 Pointer_group_interface__extract_grobs (me
, (Grob
*)0, "note-columns");
426 if (!columns
.size ())
429 return SCM_UNSPECIFIED
;
431 if (dynamic_cast<Spanner
*> (me
)->broken_b ())
433 me
->warning ("Tuplet_bracket was across linebreak. Farewell cruel world.");
435 return SCM_UNSPECIFIED
;
438 Direction dir
= get_grob_direction (me
);
441 dir
= Tuplet_bracket::get_default_dir (me
);
442 set_grob_direction (me
, dir
);
445 bool equally_long
= false;
446 Grob
* par_beam
= parallel_beam (me
, columns
, &equally_long
);
449 We follow the beam only if there is one, and we are next to it.
453 || get_grob_direction (par_beam
) != dir
)
455 calc_position_and_height (me
,&offset
,&dy
);
459 SCM ps
= par_beam
->get_grob_property ("positions");
461 Real lp
= gh_scm2double (gh_car (ps
));
462 Real rp
= gh_scm2double (gh_cdr (ps
));
467 offset
= lp
+ dir
* (0.5 + gh_scm2double (me
->get_grob_property ("padding")));
472 SCM lp
= me
->get_grob_property ("left-position");
473 SCM rp
= me
->get_grob_property ("right-position");
475 if (gh_number_p (lp
) && !gh_number_p (rp
))
477 rp
= gh_double2scm (gh_scm2double (lp
) + dy
);
479 else if (gh_number_p (rp
) && !gh_number_p (lp
))
481 lp
= gh_double2scm (gh_scm2double (rp
) - dy
);
483 else if (!gh_number_p (rp
) && !gh_number_p (lp
))
485 lp
= gh_double2scm (offset
);
486 rp
= gh_double2scm (offset
+dy
);
489 me
->set_grob_property ("left-position", lp
);
490 me
->set_grob_property ("right-position", rp
);
492 return SCM_UNSPECIFIED
;
500 Tuplet_bracket::get_default_dir (Grob
*me
)
503 for (SCM s
= me
->get_grob_property ("note-columns"); gh_pair_p (s
); s
= ly_cdr (s
))
505 Grob
* nc
= unsmob_grob (ly_car (s
));
506 if (Note_column::dir (nc
) < 0)
516 Tuplet_bracket::add_column (Grob
*me
, Item
*n
)
518 Pointer_group_interface::add_grob (me
, ly_symbol2scm ("note-columns"), n
);
519 me
->add_dependency (n
);
521 add_bound_item (dynamic_cast<Spanner
*> (me
), n
);
529 ADD_INTERFACE (Tuplet_bracket
,"tuplet-bracket-interface",
530 "A bracket with a number in the middle, used for tuplets.",
531 "note-columns bracket-flare edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");