]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-bracket.cc
* lily/main.cc (setup_guile_env): new function. Set GC min_yields
[lilypond.git] / lily / tuplet-bracket.cc
1 /*
2   tuplet-bracket.cc -- implement Tuplet_bracket
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 /*
11   TODO:
12
13   - tuplet bracket should probably be subject to the same rules as
14   beam sloping/quanting.
15
16   - There is no support for kneed brackets, or nested brackets.
17
18   - number placement for parallel beams should be much more advanced:
19   for sloped beams some extra horizontal offset must be introduced.
20
21   - number placement is usually done over the center note, not the
22   graphical center.
23 */
24
25 /*
26   TODO: quantise, we don't want to collide with staff lines.
27   (or should we be above staff?)
28
29   todo: handle breaking elegantly.
30 */
31
32 #include <math.h>
33
34 #include "tuplet-bracket.hh"
35 #include "line-interface.hh"
36 #include "beam.hh"
37 #include "warn.hh"
38 #include "font-interface.hh"
39 #include "output-def.hh"
40 #include "text-interface.hh"
41 #include "stem.hh"
42 #include "note-column.hh"
43 #include "pointer-group-interface.hh"
44 #include "directional-element-interface.hh"
45 #include "spanner.hh"
46 #include "staff-symbol-referencer.hh"
47 #include "lookup.hh"
48
49 static Grob *
50 get_x_bound_grob (Grob *g, Direction my_dir)
51 {
52   if (Note_column::get_stem (g)
53       && Note_column::dir (g) == my_dir)
54     {
55       g = Note_column::get_stem (g);
56     }
57   return g;
58 }
59
60 Grob *
61 Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
62 {
63   /*
64     ugh: code dup.
65   */
66   Grob *s1 = Note_column::get_stem (cols[0]);
67   Grob *s2 = Note_column::get_stem (cols.top ());
68
69   Grob *b1 = s1 ? Stem::get_beam (s1) : 0;
70   Grob *b2 = s2 ? Stem::get_beam (s2) : 0;
71
72   Spanner *sp = dynamic_cast<Spanner *> (me);
73
74   *equally_long = false;
75   if (! (b1 && (b1 == b2) && !sp->is_broken ()))
76     return 0;
77
78   extract_grob_set (b1, "stems", beam_stems);
79   if (beam_stems.size () == 0)
80     {
81       programming_error ("beam under tuplet bracket has no stems");
82       *equally_long = 0;
83       return 0;
84     }
85
86   *equally_long = (beam_stems[0] == s1 && beam_stems.top () == s2);
87   return b1;
88 }
89
90 /*
91   TODO:
92
93   in the case that there is no bracket, but there is a (single) beam,
94   follow beam precisely for determining tuplet number location.
95 */
96 MAKE_SCHEME_CALLBACK (Tuplet_bracket, print, 1);
97 SCM
98 Tuplet_bracket::print (SCM smob)
99 {
100   Grob *me = unsmob_grob (smob);
101   Stencil mol;
102   extract_grob_set (me, "note-columns", columns);
103
104   if (!columns.size ())
105     return mol.smobbed_copy ();
106
107   {
108     SCM lp = me->get_property ("left-position");
109     SCM rp = me->get_property ("right-position");
110
111     if (!scm_is_number (rp) || !scm_is_number (lp))
112       after_line_breaking (smob);
113   }
114
115   Real ly = robust_scm2double (me->get_property ("left-position"), 0);
116   Real ry = robust_scm2double (me->get_property ("right-position"), 0);
117
118   bool equally_long = false;
119   Grob *par_beam = parallel_beam (me, columns, &equally_long);
120
121   Spanner *sp = dynamic_cast<Spanner *> (me);
122
123   bool bracket_visibility = !(par_beam && equally_long);
124   bool number_visibility = true;
125
126   /*
127     Fixme: the type of this prop is sucky.
128   */
129   SCM bracket = me->get_property ("bracket-visibility");
130   if (scm_is_bool (bracket))
131     {
132       bracket_visibility = ly_scm2bool (bracket);
133     }
134   else if (bracket == ly_symbol2scm ("if-no-beam"))
135     bracket_visibility = !par_beam;
136
137   SCM numb = me->get_property ("number-visibility");
138   if (scm_is_bool (numb))
139     {
140       number_visibility = ly_scm2bool (numb);
141     }
142   else if (numb == ly_symbol2scm ("if-no-beam"))
143     number_visibility = !par_beam;
144
145   Grob *commonx = columns[0]->common_refpoint (columns.top (), X_AXIS);
146
147   /*
148     Tuplet brackets are normally not broken, but we shouldn't crash if
149     they are.
150   */
151   commonx = commonx->common_refpoint (sp->get_bound (LEFT), X_AXIS);
152   commonx = commonx->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
153
154   Direction dir = get_grob_direction (me);
155
156   Grob *lgr = get_x_bound_grob (columns[0], dir);
157   Grob *rgr = get_x_bound_grob (columns.top (), dir);
158
159   Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
160   Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
161   Real w = x1 -x0;
162
163   SCM number = me->get_property ("text");
164
165   Output_def *pap = me->get_layout ();
166   Stencil num;
167   if (scm_is_string (number) && number_visibility)
168     {
169       SCM properties = Font_interface::text_font_alist_chain (me);
170       SCM snum = Text_interface::interpret_markup (pap->self_scm (), properties, number);
171       num = *unsmob_stencil (snum);
172       num.align_to (X_AXIS, CENTER);
173       num.translate_axis (w / 2, X_AXIS);
174       num.align_to (Y_AXIS, CENTER);
175
176       num.translate_axis ((ry - ly) / 2, Y_AXIS);
177
178       mol.add_stencil (num);
179     }
180
181   /*
182     No bracket when it would be smaller than the number.
183
184     TODO: should use GAP in calculation too.
185   */
186   if (bracket_visibility && number_visibility
187       && mol.extent (X_AXIS).length () > w)
188     {
189       bracket_visibility = false;
190     }
191
192   if (bracket_visibility)
193     {
194       Real ss = Staff_symbol_referencer::staff_space (me);
195       Real gap = 0.;
196
197       if (!num.extent (X_AXIS).is_empty ())
198         gap = num.extent (X_AXIS).length () + 1.0;
199
200       SCM fl = me->get_property ("bracket-flare");
201       SCM eh = me->get_property ("edge-height");
202       SCM sp = me->get_property ("shorten-pair");
203
204       Direction d = LEFT;
205       Drul_array<Real> height, flare, shorten;
206       do
207         {
208           flare[d] = height[d] = shorten[d] = 0.0;
209           if (is_number_pair (fl))
210             flare[d] += ss * scm_to_double (index_get_cell (fl, d));
211           if (is_number_pair (eh))
212             height[d] += -dir * ss * scm_to_double (index_get_cell (eh, d));
213           if (is_number_pair (sp))
214             shorten[d] += ss * scm_to_double (index_get_cell (sp, d));
215         }
216       while (flip (&d) != LEFT);
217
218       Stencil brack = make_bracket (me, Y_AXIS,
219                                     Offset (w, ry - ly),
220                                     height,
221                                     /*
222                                       0.1 = more space at right due to italics
223                                       TODO: use italic correction of font.
224                                     */
225                                     Interval (-0.5, 0.5) * gap + 0.1,
226                                     flare, shorten);
227       mol.add_stencil (brack);
228     }
229
230   mol.translate_axis (ly, Y_AXIS);
231   mol.translate_axis (x0 - sp->get_bound (LEFT)->relative_coordinate (commonx, X_AXIS), X_AXIS);
232   return mol.smobbed_copy ();
233 }
234
235 /*
236   should move to lookup?
237
238   TODO: this will fail for very short (shorter than the flare)
239   brackets.
240 */
241 Stencil
242 Tuplet_bracket::make_bracket (Grob *me, // for line properties.
243                               Axis protusion_axis,
244                               Offset dz,
245                               Drul_array<Real> height,
246                               Interval gap,
247                               Drul_array<Real> flare,
248                               Drul_array<Real> shorten)
249 {
250   Drul_array<Offset> corners (Offset (0, 0), dz);
251
252   Real length = dz.length ();
253   Drul_array<Offset> gap_corners;
254
255   Axis bracket_axis = other_axis (protusion_axis);
256
257   Drul_array<Offset> straight_corners = corners;
258
259   Direction d = LEFT;
260   do
261     {
262       straight_corners[d] += -d * shorten[d] / length * dz;
263     }
264   while (flip (&d) != LEFT);
265
266   if (gap.is_empty ())
267     gap = Interval (0, 0);
268   do
269     {
270       gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
271     }
272   while (flip (&d) != LEFT);
273
274   Drul_array<Offset> flare_corners = straight_corners;
275   do
276     {
277       flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
278       flare_corners[d][protusion_axis] += height[d];
279       straight_corners[d][bracket_axis] += -d * flare[d];
280     }
281   while (flip (&d) != LEFT);
282
283   Stencil m;
284   do
285     {
286       m.add_stencil (Line_interface::line (me, straight_corners[d],
287                                            gap_corners[d]));
288
289       m.add_stencil (Line_interface::line (me, straight_corners[d],
290                                            flare_corners[d]));
291     }
292   while (flip (&d) != LEFT);
293
294   return m;
295 }
296
297 /*
298   use first -> last note for slope, and then correct for disturbing
299   notes in between.  */
300 void
301 Tuplet_bracket::calc_position_and_height (Grob *me, Real *offset, Real *dy)
302 {
303   extract_grob_set (me, "note-columns", columns);
304   Grob *commony = common_refpoint_of_array (columns, me, Y_AXIS);
305   Grob *commonx = common_refpoint_of_array (columns, me, X_AXIS);
306
307   Interval staff;
308   if (Grob *st = Staff_symbol_referencer::get_staff_symbol (me))
309     staff = st->extent (commony, Y_AXIS);
310
311   Direction dir = get_grob_direction (me);
312
313   /*
314     Use outer non-rest columns to determine slope
315   */
316   int l = 0;
317   while (l < columns.size () && Note_column::has_rests (columns[l]))
318     l++;
319
320   int r = columns.size ()- 1;
321   while (r >= l && Note_column::has_rests (columns[r]))
322     r--;
323
324   if (l < r)
325     {
326       Interval rv = columns[r]->extent (commony, Y_AXIS);
327       Interval lv = columns[l]->extent (commony, Y_AXIS);
328       rv.unite (staff);
329       lv.unite (staff);
330       Real graphical_dy = rv[dir] - lv[dir];
331
332       Slice ls = Note_column::head_positions_interval (columns[l]);
333       Slice rs = Note_column::head_positions_interval (columns[r]);
334
335       Interval musical_dy;
336       musical_dy[UP] = rs[UP] - ls[UP];
337       musical_dy[DOWN] = rs[DOWN] - ls[DOWN];
338       if (sign (musical_dy[UP]) != sign (musical_dy[DOWN]))
339         *dy = 0.0;
340       else if (sign (graphical_dy) != sign (musical_dy[DOWN]))
341         *dy = 0.0;
342       else
343         *dy = graphical_dy;
344     }
345   else
346     * dy = 0;
347
348   *offset = -dir * infinity_f;
349
350   if (!columns.size ())
351     return;
352
353   Grob *lgr = get_x_bound_grob (columns[0], dir);
354   Grob *rgr = get_x_bound_grob (columns.top (), dir);
355   Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
356   Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
357
358   /*
359     Slope.
360   */
361   Real factor = columns.size () > 1 ? 1 / (x1 - x0) : 1.0;
362
363   for (int i = 0; i < columns.size (); i++)
364     {
365       Interval note_ext = columns[i]->extent (commony, Y_AXIS);
366       note_ext.unite (staff);
367       Real notey = note_ext[dir] - me->relative_coordinate (commony, Y_AXIS);
368
369       Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
370       Real tuplety = *dy * x * factor;
371
372       if (notey * dir > (*offset + tuplety) * dir)
373         *offset = notey - tuplety;
374     }
375
376   // padding
377   *offset += scm_to_double (me->get_property ("padding")) * dir;
378
379   /*
380     horizontal brackets should not collide with staff lines.
381
382   */
383   Real ss = Staff_symbol_referencer::staff_space (me);
384   if (*dy == 0 && fabs (*offset) < ss * Staff_symbol_referencer::staff_radius (me))
385     {
386       // quantize, then do collision check.
387       *offset *= 2 / ss;
388
389       *offset = rint (*offset);
390       if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
391         *offset += dir;
392
393       *offset *= 0.5 * ss;
394     }
395 }
396
397 /*
398   We depend on the beams if there are any.
399 */
400 MAKE_SCHEME_CALLBACK (Tuplet_bracket, before_line_breaking, 1);
401 SCM
402 Tuplet_bracket::before_line_breaking (SCM smob)
403 {
404   Grob *me = unsmob_grob (smob);
405   extract_grob_set (me, "note-columns", columns);
406
407   for (int i = columns.size (); i--;)
408     {
409       Grob *s = Note_column::get_stem (columns[i]);
410       Grob *b = s ? Stem::get_beam (s) : 0;
411       if (b)
412         me->add_dependency (b);
413     }
414   return SCM_UNSPECIFIED;
415 }
416
417 MAKE_SCHEME_CALLBACK (Tuplet_bracket, after_line_breaking, 1);
418
419 SCM
420 Tuplet_bracket::after_line_breaking (SCM smob)
421 {
422   Grob *me = unsmob_grob (smob);
423   extract_grob_set (me, "note-columns", columns);
424
425   if (!columns.size ())
426     {
427       me->suicide ();
428       return SCM_UNSPECIFIED;
429     }
430   if (dynamic_cast<Spanner *> (me)->is_broken ())
431     {
432       me->warning (_ ("removing tuplet bracket across linebreak"));
433       me->suicide ();
434       return SCM_UNSPECIFIED;
435     }
436
437   Direction dir = get_grob_direction (me);
438   if (!dir)
439     {
440       dir = Tuplet_bracket::get_default_dir (me);
441       set_grob_direction (me, dir);
442     }
443
444   bool equally_long = false;
445   Grob *par_beam = parallel_beam (me, columns, &equally_long);
446
447   /*
448     We follow the beam only if there is one, and we are next to it.
449   */
450   Real dy, offset;
451   if (!par_beam
452       || get_grob_direction (par_beam) != dir)
453     {
454       calc_position_and_height (me, &offset, &dy);
455     }
456   else
457     {
458       SCM ps = par_beam->get_property ("positions");
459
460       Real lp = scm_to_double (scm_car (ps));
461       Real rp = scm_to_double (scm_cdr (ps));
462
463       /*
464         duh. magic.
465       */
466       offset = lp + dir * (0.5 + scm_to_double (me->get_property ("padding")));
467       dy = rp- lp;
468     }
469
470   SCM lp = me->get_property ("left-position");
471   SCM rp = me->get_property ("right-position");
472
473   if (scm_is_number (lp) && !scm_is_number (rp))
474     {
475       rp = scm_from_double (scm_to_double (lp) + dy);
476     }
477   else if (scm_is_number (rp) && !scm_is_number (lp))
478     {
479       lp = scm_from_double (scm_to_double (rp) - dy);
480     }
481   else if (!scm_is_number (rp) && !scm_is_number (lp))
482     {
483       lp = scm_from_double (offset);
484       rp = scm_from_double (offset + dy);
485     }
486
487   me->set_property ("left-position", lp);
488   me->set_property ("right-position", rp);
489
490   return SCM_UNSPECIFIED;
491 }
492
493 /*
494   similar to beam ?
495 */
496 Direction
497 Tuplet_bracket::get_default_dir (Grob *me)
498 {
499   Drul_array<int> dirs (0, 0);
500   extract_grob_set (me, "note-columns", columns);
501   for (int i = 0 ; i < columns.size (); i++)
502     {
503       Grob *nc = columns[i];
504       Direction d = Note_column::dir (nc);
505       if (d)
506         dirs[d]++;
507     }
508
509   return dirs[UP] >= dirs[DOWN] ? UP : DOWN;
510 }
511
512 void
513 Tuplet_bracket::add_column (Grob *me, Item *n)
514 {
515   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
516   me->add_dependency (n);
517
518   add_bound_item (dynamic_cast<Spanner *> (me), n);
519 }
520
521 ADD_INTERFACE (Tuplet_bracket,
522                "tuplet-bracket-interface",
523                "A bracket with a number in the middle, used for tuplets.",
524
525                "note-columns bracket-flare edge-height shorten-pair "
526                "padding left-position right-position bracket-visibility "
527                "number-visibility thickness direction");
528