]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-bracket.cc
*** empty log message ***
[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-item.hh"
41 #include "stem.hh"
42 #include "note-column.hh"
43 #include "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   Link_array<Grob> beam_stems = extract_grob_array (b1, ly_symbol2scm ("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   Link_array<Grob> columns
103     = extract_grob_array (me, ly_symbol2scm ("note-columns"));
104
105   if (!columns.size ())
106     return mol.smobbed_copy ();
107
108   {
109     SCM lp = me->get_property ("left-position");
110     SCM rp = me->get_property ("right-position");
111
112     if (!scm_is_number (rp) || !scm_is_number (lp))
113       after_line_breaking (smob);
114   }
115
116   Real ly = robust_scm2double (me->get_property ("left-position"), 0);
117   Real ry = robust_scm2double (me->get_property ("right-position"), 0);
118
119   bool equally_long = false;
120   Grob *par_beam = parallel_beam (me, columns, &equally_long);
121
122   Spanner *sp = dynamic_cast<Spanner *> (me);
123
124   bool bracket_visibility = !(par_beam && equally_long);
125   bool number_visibility = true;
126
127   /*
128     Fixme: the type of this prop is sucky.
129   */
130   SCM bracket = me->get_property ("bracket-visibility");
131   if (scm_is_bool (bracket))
132     {
133       bracket_visibility = ly_scm2bool (bracket);
134     }
135   else if (bracket == ly_symbol2scm ("if-no-beam"))
136     bracket_visibility = !par_beam;
137
138   SCM numb = me->get_property ("number-visibility");
139   if (scm_is_bool (numb))
140     {
141       number_visibility = ly_scm2bool (numb);
142     }
143   else if (numb == ly_symbol2scm ("if-no-beam"))
144     number_visibility = !par_beam;
145
146   Grob *commonx = columns[0]->common_refpoint (columns.top (), X_AXIS);
147
148   /*
149     Tuplet brackets are normally not broken, but we shouldn't crash if
150     they are.
151   */
152   commonx = commonx->common_refpoint (sp->get_bound (LEFT), X_AXIS);
153   commonx = commonx->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
154
155   Direction dir = get_grob_direction (me);
156
157   Grob *lgr = get_x_bound_grob (columns[0], dir);
158   Grob *rgr = get_x_bound_grob (columns.top (), dir);
159
160   Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
161   Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
162   Real w = x1 -x0;
163
164   SCM number = me->get_property ("text");
165
166   Output_def *pap = me->get_layout ();
167   Stencil num;
168   if (scm_is_string (number) && number_visibility)
169     {
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);
176
177       num.translate_axis ((ry - ly) / 2, Y_AXIS);
178
179       mol.add_stencil (num);
180     }
181
182   /*
183     No bracket when it would be smaller than the number.
184
185     TODO: should use GAP in calculation too.
186   */
187   if (bracket_visibility && number_visibility
188       && mol.extent (X_AXIS).length () > w)
189     {
190       bracket_visibility = false;
191     }
192
193   if (bracket_visibility)
194     {
195       Real ss = Staff_symbol_referencer::staff_space (me);
196       Real gap = 0.;
197
198       if (!num.extent (X_AXIS).is_empty ())
199         gap = num.extent (X_AXIS).length () + 1.0;
200
201       SCM fl = me->get_property ("bracket-flare");
202       SCM eh = me->get_property ("edge-height");
203       SCM sp = me->get_property ("shorten-pair");
204
205       Direction d = LEFT;
206       Drul_array<Real> height, flare, shorten;
207       do
208         {
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));
216         }
217       while (flip (&d) != LEFT);
218
219       Stencil brack = make_bracket (me, Y_AXIS,
220                                     Offset (w, ry - ly),
221                                     height,
222                                     /*
223                                       0.1 = more space at right due to italics
224                                       TODO: use italic correction of font.
225                                     */
226                                     Interval (-0.5, 0.5) * gap + 0.1,
227                                     flare, shorten);
228       mol.add_stencil (brack);
229     }
230
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 ();
234 }
235
236 /*
237   should move to lookup?
238
239   TODO: this will fail for very short (shorter than the flare)
240   brackets.
241 */
242 Stencil
243 Tuplet_bracket::make_bracket (Grob *me, // for line properties.
244                               Axis protusion_axis,
245                               Offset dz,
246                               Drul_array<Real> height,
247                               Interval gap,
248                               Drul_array<Real> flare,
249                               Drul_array<Real> shorten)
250 {
251   Drul_array<Offset> corners (Offset (0, 0), dz);
252
253   Real length = dz.length ();
254   Drul_array<Offset> gap_corners;
255
256   Axis bracket_axis = other_axis (protusion_axis);
257
258   Drul_array<Offset> straight_corners = corners;
259
260   Direction d = LEFT;
261   do
262     {
263       straight_corners[d] += -d * shorten[d] / length * dz;
264     }
265   while (flip (&d) != LEFT);
266
267   if (gap.is_empty ())
268     gap = Interval (0, 0);
269   do
270     {
271       gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
272     }
273   while (flip (&d) != LEFT);
274
275   Drul_array<Offset> flare_corners = straight_corners;
276   do
277     {
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];
281     }
282   while (flip (&d) != LEFT);
283
284   Stencil m;
285   do
286     {
287       m.add_stencil (Line_interface::line (me, straight_corners[d],
288                                            gap_corners[d]));
289
290       m.add_stencil (Line_interface::line (me, straight_corners[d],
291                                            flare_corners[d]));
292     }
293   while (flip (&d) != LEFT);
294
295   return m;
296 }
297
298 /*
299   use first -> last note for slope, and then correct for disturbing
300   notes in between.  */
301 void
302 Tuplet_bracket::calc_position_and_height (Grob *me, Real *offset, Real *dy)
303 {
304   Link_array<Grob> columns
305     = extract_grob_array (me, ly_symbol2scm ("note-columns"));
306
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);
310
311   Interval staff;
312   if (Grob *st = Staff_symbol_referencer::get_staff_symbol (me))
313     staff = st->extent (commony, Y_AXIS);
314
315   Direction dir = get_grob_direction (me);
316
317   /*
318     Use outer non-rest columns to determine slope
319   */
320   int l = 0;
321   while (l < columns.size () && Note_column::has_rests (columns[l]))
322     l++;
323
324   int r = columns.size ()- 1;
325   while (r >= l && Note_column::has_rests (columns[r]))
326     r--;
327
328   if (l < r)
329     {
330       Interval rv = columns[r]->extent (commony, Y_AXIS);
331       Interval lv = columns[l]->extent (commony, Y_AXIS);
332       rv.unite (staff);
333       lv.unite (staff);
334       Real graphical_dy = rv[dir] - lv[dir];
335
336       Slice ls = Note_column::head_positions_interval (columns[l]);
337       Slice rs = Note_column::head_positions_interval (columns[r]);
338
339       Interval musical_dy;
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]))
343         *dy = 0.0;
344       else if (sign (graphical_dy) != sign (musical_dy[DOWN]))
345         *dy = 0.0;
346       else
347         *dy = graphical_dy;
348     }
349   else
350     * dy = 0;
351
352   *offset = -dir * infinity_f;
353
354   if (!columns.size ())
355     return;
356
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];
361
362   /*
363     Slope.
364   */
365   Real factor = columns.size () > 1 ? 1 / (x1 - x0) : 1.0;
366
367   for (int i = 0; i < columns.size (); i++)
368     {
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);
372
373       Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
374       Real tuplety = *dy * x * factor;
375
376       if (notey * dir > (*offset + tuplety) * dir)
377         *offset = notey - tuplety;
378     }
379
380   // padding
381   *offset += scm_to_double (me->get_property ("padding")) * dir;
382
383   /*
384     horizontal brackets should not collide with staff lines.
385
386   */
387   Real ss = Staff_symbol_referencer::staff_space (me);
388   if (*dy == 0 && fabs (*offset) < ss * Staff_symbol_referencer::staff_radius (me))
389     {
390       // quantize, then do collision check.
391       *offset *= 2 / ss;
392
393       *offset = rint (*offset);
394       if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
395         *offset += dir;
396
397       *offset *= 0.5 * ss;
398     }
399 }
400
401 /*
402   We depend on the beams if there are any.
403 */
404 MAKE_SCHEME_CALLBACK (Tuplet_bracket, before_line_breaking, 1);
405 SCM
406 Tuplet_bracket::before_line_breaking (SCM smob)
407 {
408   Grob *me = unsmob_grob (smob);
409   Link_array<Grob> columns
410     = extract_grob_array (me, ly_symbol2scm ("note-columns"));
411
412   for (int i = columns.size (); i--;)
413     {
414       Grob *s = Note_column::get_stem (columns[i]);
415       Grob *b = s ? Stem::get_beam (s) : 0;
416       if (b)
417         me->add_dependency (b);
418     }
419   return SCM_UNSPECIFIED;
420 }
421
422 MAKE_SCHEME_CALLBACK (Tuplet_bracket, after_line_breaking, 1);
423
424 SCM
425 Tuplet_bracket::after_line_breaking (SCM smob)
426 {
427   Grob *me = unsmob_grob (smob);
428   Link_array<Grob> columns
429     = extract_grob_array (me, ly_symbol2scm ("note-columns"));
430
431   if (!columns.size ())
432     {
433       me->suicide ();
434       return SCM_UNSPECIFIED;
435     }
436   if (dynamic_cast<Spanner *> (me)->is_broken ())
437     {
438       me->warning (_ ("removing tuplet bracket across linebreak"));
439       me->suicide ();
440       return SCM_UNSPECIFIED;
441     }
442
443   Direction dir = get_grob_direction (me);
444   if (!dir)
445     {
446       dir = Tuplet_bracket::get_default_dir (me);
447       set_grob_direction (me, dir);
448     }
449
450   bool equally_long = false;
451   Grob *par_beam = parallel_beam (me, columns, &equally_long);
452
453   /*
454     We follow the beam only if there is one, and we are next to it.
455   */
456   Real dy, offset;
457   if (!par_beam
458       || get_grob_direction (par_beam) != dir)
459     {
460       calc_position_and_height (me, &offset, &dy);
461     }
462   else
463     {
464       SCM ps = par_beam->get_property ("positions");
465
466       Real lp = scm_to_double (scm_car (ps));
467       Real rp = scm_to_double (scm_cdr (ps));
468
469       /*
470         duh. magic.
471       */
472       offset = lp + dir * (0.5 + scm_to_double (me->get_property ("padding")));
473       dy = rp- lp;
474     }
475
476   SCM lp = me->get_property ("left-position");
477   SCM rp = me->get_property ("right-position");
478
479   if (scm_is_number (lp) && !scm_is_number (rp))
480     {
481       rp = scm_make_real (scm_to_double (lp) + dy);
482     }
483   else if (scm_is_number (rp) && !scm_is_number (lp))
484     {
485       lp = scm_make_real (scm_to_double (rp) - dy);
486     }
487   else if (!scm_is_number (rp) && !scm_is_number (lp))
488     {
489       lp = scm_make_real (offset);
490       rp = scm_make_real (offset + dy);
491     }
492
493   me->set_property ("left-position", lp);
494   me->set_property ("right-position", rp);
495
496   return SCM_UNSPECIFIED;
497 }
498
499 /*
500   similar to beam ?
501 */
502 Direction
503 Tuplet_bracket::get_default_dir (Grob *me)
504 {
505   Drul_array<int> dirs (0, 0);
506   for (SCM s = me->get_property ("note-columns"); scm_is_pair (s); s = scm_cdr (s))
507     {
508       Grob *nc = unsmob_grob (scm_car (s));
509       Direction d = Note_column::dir (nc);
510       if (d)
511         dirs[d]++;
512     }
513
514   return dirs[UP] >= dirs[DOWN] ? UP : DOWN;
515 }
516
517 void
518 Tuplet_bracket::add_column (Grob *me, Item *n)
519 {
520   Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
521   me->add_dependency (n);
522
523   add_bound_item (dynamic_cast<Spanner *> (me), n);
524 }
525
526 ADD_INTERFACE (Tuplet_bracket,
527                "tuplet-bracket-interface",
528                "A bracket with a number in the middle, used for tuplets.",
529
530                "note-columns bracket-flare edge-height shorten-pair "
531                "padding left-position right-position bracket-visibility "
532                "number-visibility thickness direction");
533