]> git.donarmstrong.com Git - lilypond.git/blob - lily/tuplet-bracket.cc
The grand \paper -> \layout, \bookpaper -> \paper renaming.
[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 <math.h>
27
28 #include "line-interface.hh"
29 #include "beam.hh"
30 #include "box.hh"
31 #include "warn.hh"
32 #include "font-interface.hh"
33 #include "stencil.hh"
34 #include "output-def.hh"
35 #include "text-item.hh"
36 #include "tuplet-bracket.hh"
37 #include "stem.hh"
38 #include "note-column.hh"
39 #include "group-interface.hh"
40 #include "directional-element-interface.hh"
41 #include "spanner.hh"
42 #include "staff-symbol-referencer.hh"
43 #include "lookup.hh"
44
45
46 static Grob*
47 get_x_bound_grob (Grob *g, Direction my_dir)
48 {
49   if (Note_column::get_stem (g)
50       && Note_column::dir (g) == my_dir)
51     {
52       g = Note_column::get_stem (g);
53     }
54   return g;
55 }
56
57
58
59 Grob*
60 Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
61 {
62   /*
63     ugh: code dup. 
64   */
65   Grob *s1 = Note_column::get_stem (cols[0]); 
66   Grob *s2 = Note_column::get_stem (cols.top ());    
67
68   Grob*b1 = s1 ? Stem::get_beam (s1) : 0;
69   Grob*b2 = s2 ? Stem::get_beam (s2) : 0;
70   
71   Spanner*sp = dynamic_cast<Spanner*> (me);  
72
73   *equally_long= false;
74   if (! (b1 && (b1 == b2) && !sp->is_broken ()))
75       return 0;
76
77   Link_array<Grob> beam_stems = Pointer_group_interface__extract_grobs
78     (b1, (Grob*)0, "stems");
79
80   if (beam_stems.size () == 0)
81     {
82       programming_error ("Beam under tuplet bracket has no stems!");
83       *equally_long = 0;
84       return 0;
85     }
86   
87   *equally_long = (beam_stems[0] == s1 && beam_stems.top () == s2);
88   return b1;
89 }
90
91
92 /*
93   TODO:
94
95   in the case that there is no bracket, but there is a (single) beam,
96   follow beam precisely for determining tuplet number location.
97   
98  */
99 MAKE_SCHEME_CALLBACK (Tuplet_bracket,print,1);
100 SCM
101 Tuplet_bracket::print (SCM smob) 
102 {
103   Grob *me= unsmob_grob (smob);
104   Stencil  mol;
105   Link_array<Grob> columns=
106     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
107
108   if (!columns.size ())
109     return mol.smobbed_copy ();
110
111   
112
113   {
114     SCM lp = me->get_property ("left-position");
115     SCM rp = me->get_property ("right-position");  
116
117     if (!scm_is_number (rp) || !scm_is_number (lp))
118       after_line_breaking (smob);
119   }
120   
121   Real ly = robust_scm2double (me->get_property ("left-position"), 0);
122   Real ry = robust_scm2double (me->get_property ("right-position"), 0);  
123   
124   bool equally_long = false;
125   Grob * par_beam = parallel_beam (me, columns, &equally_long);
126
127   Spanner*sp = dynamic_cast<Spanner*> (me);  
128
129   bool bracket_visibility = !(par_beam && equally_long);
130   bool number_visibility = true;
131
132   /*
133     Fixme: the type of this prop is sucky.
134    */
135   SCM bracket = me->get_property ("bracket-visibility");
136   if (scm_is_bool (bracket))
137     {
138       bracket_visibility = ly_scm2bool (bracket);
139     }
140   else if (bracket == ly_symbol2scm ("if-no-beam"))
141     bracket_visibility = !par_beam;
142
143   SCM numb = me->get_property ("number-visibility");  
144   if (scm_is_bool (numb))
145     {
146       number_visibility = ly_scm2bool (numb);
147     }
148   else if (numb == ly_symbol2scm ("if-no-beam"))
149     number_visibility = !par_beam;
150         
151   Grob * commonx = columns[0]->common_refpoint (columns.top (),X_AXIS);
152
153   /*
154     Tuplet brackets are normally not broken, but we shouldn't crash if
155     they are.
156    */
157   commonx = commonx->common_refpoint (sp->get_bound (LEFT), X_AXIS);
158   commonx = commonx->common_refpoint (sp->get_bound (RIGHT), X_AXIS);  
159   
160   Direction dir = get_grob_direction (me);
161
162   Grob * lgr = get_x_bound_grob (columns[0], dir);
163   Grob * rgr = get_x_bound_grob (columns.top (), dir);
164   
165   Real x0 = robust_relative_extent (lgr, commonx, X_AXIS)[LEFT];
166   Real x1 = robust_relative_extent (rgr, commonx, X_AXIS)[RIGHT];
167   Real w = x1 -x0;
168   
169   SCM number = me->get_property ("text");
170
171   Output_def *pap = me->get_layout ();
172   Stencil num;
173  if (scm_is_string (number) && number_visibility)
174     {
175       SCM properties = Font_interface::text_font_alist_chain (me);
176       SCM snum = Text_interface::interpret_markup (pap->self_scm (), properties, number);
177       num = *unsmob_stencil (snum);
178       num.align_to (X_AXIS, CENTER);
179       num.translate_axis (w/2, X_AXIS);
180       num.align_to (Y_AXIS, CENTER);
181         
182       num.translate_axis ((ry-ly)/2, Y_AXIS);
183
184       mol.add_stencil (num);
185     }
186
187
188   /*
189     No bracket when it would be smaller than the number.
190
191     TODO: should use GAP in calculation too.
192    */
193   if (bracket_visibility && number_visibility
194       && mol.extent (X_AXIS).length () > w)
195     {
196       bracket_visibility = false;
197     }
198   
199   if (bracket_visibility)      
200     {
201       Real ss =   Staff_symbol_referencer::staff_space (me);
202       Real gap=0.;
203
204       if (!num.extent (X_AXIS).is_empty ())
205         gap = num.extent (X_AXIS).length () + 1.0;
206       
207       SCM fl = me->get_property ("bracket-flare");
208       SCM eh = me->get_property ("edge-height");
209       SCM sp = me->get_property ("shorten-pair");
210       
211       Direction d = LEFT;
212       Drul_array<Real> height, flare, shorten;
213       do {
214         flare[d] =  height[d] = shorten[d] = 0.0;
215         if (is_number_pair (fl))
216           flare[d] +=  ss * scm_to_double (index_get_cell (fl, d));
217         if (is_number_pair (eh))
218           height[d] +=  - dir * ss *scm_to_double (index_get_cell (eh, d));
219         if (is_number_pair (sp))
220           shorten[d] +=  ss *scm_to_double (index_get_cell (sp, d));
221       }
222       while (flip (&d) != LEFT);
223       
224       Stencil brack = make_bracket (me, Y_AXIS,
225                                      Offset (w, ry - ly), 
226                                      height,
227                                     /*
228                                       0.1 = more space at right due to italics
229                                       TODO: use italic correction of font.
230                                      */
231                                     Interval (-0.5, 0.5) * gap + 0.1,
232                                      flare, shorten);
233       mol.add_stencil (brack);
234     }
235   
236   mol.translate_axis (ly, Y_AXIS);
237   mol.translate_axis (x0  - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
238   return mol.smobbed_copy ();
239 }
240
241 /*
242   should move to lookup?
243
244   TODO: this will fail for very short (shorter than the flare)
245   brackets.
246  */
247 Stencil
248 Tuplet_bracket::make_bracket (Grob *me, // for line properties.
249                               Axis protusion_axis,
250                               Offset dz,
251                               Drul_array<Real> height,
252                               Interval gap,
253                               Drul_array<Real> flare,
254                               Drul_array<Real> shorten)
255 {
256   Drul_array<Offset> corners (Offset (0,0), dz);
257   
258   Real length = dz.length ();
259   Drul_array<Offset> gap_corners;
260
261   
262   Axis bracket_axis = other_axis (protusion_axis);
263
264   Drul_array<Offset> straight_corners = corners;
265
266   Direction d = LEFT;
267   do {
268     straight_corners[d] += - d * shorten[d] /length * dz;
269   } while (flip (&d) != LEFT);
270
271
272   if (gap.is_empty())
273     gap = Interval (0,0);
274   do {
275     gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
276   } while (flip (&d) != LEFT);
277
278   Drul_array<Offset> flare_corners = straight_corners;
279   do {
280     flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
281     flare_corners[d][protusion_axis] += height[d];
282     straight_corners[d][bracket_axis] += - d * flare[d];
283   } while (flip (&d) != LEFT);
284
285   Stencil m;
286   do {
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   } while (flip (&d) != LEFT);
293
294   return m;  
295 }
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     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
306
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     Pointer_group_interface__extract_grobs (me, (Grob*)0, "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     Pointer_group_interface__extract_grobs (me, (Grob*)0, "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,"tuplet-bracket-interface",
543   "A bracket with a number in the middle, used for tuplets.",
544   "note-columns bracket-flare edge-height shorten-pair padding left-position right-position bracket-visibility number-visibility thickness direction");
545