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