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