]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.3.17
[lilypond.git] / lily / slur.cc
1 /*
2   slur.cc -- implement  Slur
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 /*
11   [TODO]
12     * begin and end should be treated as a/acknowledge Scripts.
13     * broken slur should have uniform trend
14  */
15
16 #include "directional-element-interface.hh"
17 #include "group-interface.hh"
18 #include "slur.hh"
19 #include "lookup.hh"
20 #include "paper-def.hh"
21 #include "note-column.hh"
22 #include "stem.hh"
23 #include "paper-column.hh"
24 #include "molecule.hh"
25 #include "debug.hh"
26 #include "box.hh"
27 #include "bezier.hh"
28 #include "main.hh"
29 #include "cross-staff.hh"
30 #include "group-interface.hh"
31
32 Slur::Slur ()
33 {
34   set_elt_property ("note-columns", SCM_EOL);
35 }
36
37 void
38 Slur::add_column (Note_column*n)
39 {
40   if (!gh_pair_p (n->get_elt_property ("note-heads")))
41     warning (_ ("Putting slur over rest.  Ignoring."));
42   else
43     {
44       Group_interface gi (this, "note-columns");
45       gi.add_element (n);
46       add_dependency (n);
47     }
48 }
49
50 Direction
51 Slur::get_default_dir () const
52 {
53   Link_array<Note_column> encompass_arr =
54     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
55   
56   Direction d = DOWN;
57   for (int i=0; i < encompass_arr.size (); i ++) 
58     {
59       if (encompass_arr[i]->dir () < 0) 
60         {
61           d = UP;
62           break;
63         }
64     }
65   return d;
66 }
67
68 void
69 Slur::do_add_processing ()
70 {
71   Link_array<Note_column> encompass_arr =
72     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
73   set_bounds (LEFT, encompass_arr[0]);    
74   if (encompass_arr.size () > 1)
75     set_bounds (RIGHT, encompass_arr.top ());
76 }
77
78 void
79 Slur::do_pre_processing ()
80 {
81   // don't set directions
82 }
83
84
85 Offset
86 Slur::encompass_offset (Note_column const* col) const
87 {
88   Offset o;
89   Stem* stem_l = col->stem_l ();
90   Direction dir = directional_element (this).get ();
91   
92   if (!stem_l)
93     {
94       warning (_ ("Slur over rest?"));
95       o[X_AXIS] = col->hpos_f ();
96       o[Y_AXIS] = col->extent (Y_AXIS)[dir];
97       return o;  
98     }
99   Direction stem_dir = directional_element (stem_l).get ();
100   o[X_AXIS] = stem_l->hpos_f ();
101
102   /*
103     Simply set x to middle of notehead
104    */
105
106   o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
107
108   if ((stem_dir == dir)
109       && !stem_l->extent (Y_AXIS).empty_b ())
110     {
111       o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
112     }
113   else
114     {
115       o[Y_AXIS] = col->extent (Y_AXIS)[dir];
116     }
117
118   /*
119    leave a gap: slur mustn't touch head/stem
120    */
121   o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
122   o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
123   return o;
124 }
125
126 /*
127   ARGRARGRARGRARGAR!
128
129   Fixme
130  */
131 void
132 Slur::do_post_processing ()
133 {
134   Link_array<Note_column> encompass_arr =
135     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
136
137   if (!encompass_arr.size ())
138     {
139       set_elt_property ("transparent", SCM_BOOL_T);
140       set_empty (X_AXIS);
141       set_empty (Y_AXIS);
142       return;
143     }
144
145   if (!directional_element (this).get ())
146     directional_element (this).set (get_default_dir ());
147
148   /* 
149    Slur and tie placement [OSU]
150
151    Slurs:
152    * x = centre of head - d * x_gap_f
153
154    TODO:
155    * y = length < 5ss : horizontal tangent + d * 0.25 ss
156      y = length >= 5ss : y next interline - d * 0.25 ss
157    */
158
159   Real staff_space = paper_l ()->get_var ("interline");
160   Real half_staff_space = staff_space / 2;
161
162   Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
163   Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
164
165   Drul_array<Note_column*> note_column_drul;
166   note_column_drul[LEFT] = encompass_arr[0];
167   note_column_drul[RIGHT] = encompass_arr.top ();
168
169   bool fix_broken_b = false;
170
171   Direction my_dir = directional_element (this).get ();
172   
173   Direction d = LEFT;
174   do 
175     {
176       dx_f_drul_[d] = 0;
177       dy_f_drul_[d] = 0;
178       
179       if ((note_column_drul[d] == spanned_drul_[d])
180           && note_column_drul[d]->first_head ()
181           && (note_column_drul[d]->stem_l ()))
182         {
183           Stem* stem_l = note_column_drul[d]->stem_l ();
184           /*
185             side directly attached to note head;
186             no beam getting in the way
187           */
188           if ((stem_l->extent (Y_AXIS).empty_b ()
189                || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
190               && !((my_dir == stem_l->get_direction ())
191                    && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
192             {
193               dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
194               dx_f_drul_[d] -= d * x_gap_f;
195
196               if (stem_l->get_direction () != my_dir)
197                 {
198                   dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
199                 }
200               else
201                 {
202                   dy_f_drul_[d] = stem_l->chord_start_f ()
203                     + my_dir * half_staff_space;
204                 }
205               dy_f_drul_[d] += my_dir * y_gap_f;
206             }
207           /*
208             side attached to (visible) stem
209           */
210           else
211             {
212               dx_f_drul_[d] = stem_l->hpos_f ()
213                 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
214               /*
215                 side attached to beamed stem
216                */
217               if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
218                 {
219                   dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
220                   dy_f_drul_[d] += my_dir * 2 * y_gap_f;
221                 }
222               /*
223                 side attached to notehead, with stem getting in the way
224                */
225               else
226                 {
227                   dx_f_drul_[d] -= d * x_gap_f;
228                   
229                   dy_f_drul_[d] = stem_l->chord_start_f ()
230                     + my_dir * half_staff_space;
231                   dy_f_drul_[d] += my_dir * y_gap_f;
232                 }
233             }
234         }
235       /*
236         loose end
237       */
238       else
239         {
240           dx_f_drul_[d] = get_broken_left_end_align ();
241                 
242           /*
243             broken: should get y from other piece, so that slur
244             continues up/down trend
245
246             for now: be horizontal..
247           */
248           fix_broken_b = true;
249         }
250     }
251   while (flip (&d) != LEFT);
252
253   int cross_count =  cross_staff_count ();
254   bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
255
256   Drul_array<Offset> info_drul;
257   Drul_array<Real> interstaff_interval;
258
259   do
260     {
261       info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
262       interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
263                                                      this);
264     }
265   while (flip (&d) != LEFT);
266   
267   Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
268
269   if (fix_broken_b)
270     {
271       Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
272         RIGHT : LEFT;
273       dy_f_drul_[d] = info_drul[d][Y_AXIS];
274       if (!interstaff_b)
275         {
276           dy_f_drul_[d] -= interstaff_interval[d];
277           if (cross_count)      // interstaff_i  ? 
278             {
279               dy_f_drul_[LEFT] += interstaff_interval[d];
280               dy_f_drul_[RIGHT] += interstaff_interval[d];
281             }
282         }
283     }
284         
285
286   /*
287     Now we've got a fine slur
288     Catch and correct some ugly cases
289    */
290   String infix = interstaff_b ? "interstaff_" : "";
291   Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
292   Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
293   Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
294   Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
295
296   if (!fix_broken_b)
297     dy_f_drul_[RIGHT] += interstaff_f;
298
299   Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
300   if (!fix_broken_b)
301     dy_f -= interstaff_f;
302   Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
303
304   /*
305     Avoid too steep slurs.
306    */
307   Real slope_ratio_f = abs (dy_f / dx_f);
308   if (slope_ratio_f > slope_damp_f)
309     {
310       Direction d = (Direction)(- my_dir * (sign (dy_f)));
311       if (!d)
312         d = LEFT;
313       Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
314       /*
315         must never change sign of dy
316        */
317       damp_f = damp_f <? abs (dy_f);
318       dy_f_drul_[d] += my_dir * damp_f;
319     }
320
321   /*
322    Avoid too high slurs 
323
324    Wierd slurs may look a lot better after they have been
325    adjusted a bit.
326    So, we'll do this in 3 steps
327    */
328   for (int i = 0; i < 3; i++)
329     {
330       Real height_f = curve_extent (Y_AXIS).length ();
331       Real width_f = curve_extent (X_AXIS).length ();
332       
333       dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
334       if (!fix_broken_b)
335         dy_f -= interstaff_f;
336
337       Real height_ratio_f = abs (height_f / width_f);
338       if (height_ratio_f > height_damp_f)
339         {
340           Direction d = (Direction)(- my_dir * (sign (dy_f)));
341           if (!d)
342             d = LEFT;
343           /* take third step */
344           Real damp_f = (height_ratio_f - height_damp_f) * width_f / 3;
345           /*
346             if y positions at about the same height, correct both ends
347           */
348           if (abs (dy_f / dx_f ) < slope_damp_f)
349             {
350               dy_f_drul_[-d] += my_dir * damp_f;
351               dy_f_drul_[d] += my_dir * damp_f;
352             }
353           /*
354             don't change slope too much, would have been catched by slope damping
355           */
356           else
357             {
358               damp_f = damp_f <? abs (dy_f/2);
359               dy_f_drul_[d] += my_dir * damp_f;
360             }
361         }
362     }
363
364   /*
365     If, after correcting, we're close to stem-end...
366   */
367   Drul_array<Real> snapy_f_drul;
368   snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
369   Drul_array<Real> snapx_f_drul;
370   snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
371   Drul_array<bool> snapped_b_drul;
372   snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
373   do
374     {
375       Note_column * nc = note_column_drul[d];
376       if (nc == spanned_drul_[d]
377           && nc->stem_l ()
378           && nc->stem_l ()->get_direction () == my_dir
379           && abs (nc->stem_l ()->extent (Y_AXIS)[my_dir]
380                   - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
381               <= snap_f)
382         {
383           /*
384             prepare to attach to stem-end
385           */
386           snapx_f_drul[d] = nc->stem_l ()->hpos_f ()
387             - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
388
389           snapy_f_drul[d] = nc->stem_l ()->extent (Y_AXIS)[my_dir]
390             + interstaff_interval[d]
391             + my_dir * 2 * y_gap_f;
392           
393           snapped_b_drul[d] = true;
394         }
395     }
396   while (flip (&d) != LEFT);
397
398   /*
399     only use snapped positions if sign (dy) will not change
400     and dy doesn't change too much
401     */
402   if (!fix_broken_b)
403     dy_f += interstaff_f;
404
405
406   /*
407     (sigh)
408
409     More refactoring could be done.
410    */
411   Real maxsnap = abs (dy_f * snap_max_dy_f);
412   if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
413       && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
414       && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
415                     < maxsnap)))
416     {
417       dy_f_drul_ = snapy_f_drul;
418       dx_f_drul_ = snapx_f_drul;
419     }
420   else
421     do
422       {
423         Direction od = (Direction)-d;
424         if (snapped_b_drul[d]
425             && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
426             && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od]  - d * dy_f)
427                           < maxsnap)))
428           {
429             dy_f_drul_[d] = snapy_f_drul[d];
430             dx_f_drul_[d] = snapx_f_drul[d];
431           }
432       }
433     while (flip (&d) != LEFT);
434 }
435
436
437 int
438 Slur::cross_staff_count ()const
439 {
440   Link_array<Note_column> encompass_arr =
441     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
442
443   int k=0;
444
445   for (int i = 0; i < encompass_arr.size (); i++)
446     {
447       if (calc_interstaff_dist (encompass_arr[i], this))
448         k++;
449     }
450   return k;
451 }
452
453
454 Array<Offset>
455 Slur::get_encompass_offset_arr () const
456 {
457   Link_array<Note_column> encompass_arr =
458     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
459   
460   Array<Offset> offset_arr;
461 #if 0
462   /*
463     check non-disturbed slur
464     FIXME: x of ends off by a tiny bit!!
465   */
466   offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
467   offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
468   return offset_arr;
469 #endif
470   
471   Offset origin (relative_coordinate (0, X_AXIS), 0);
472
473   int first = 1;
474   int last = encompass_arr.size () - 2;
475
476   offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
477
478   /*
479     left is broken edge
480   */
481
482   int cross_count  = cross_staff_count ();
483   bool cross_b = cross_count && cross_count < encompass_arr.size ();
484   if (encompass_arr[0] != spanned_drul_[LEFT])
485     {
486       first--;
487       Real is   = calc_interstaff_dist (encompass_arr[0], this);
488       if (cross_b)
489         offset_arr[0][Y_AXIS] += is;
490     }
491
492   /*
493     right is broken edge
494   */
495   if (encompass_arr.top () != spanned_drul_[RIGHT])
496     {
497       last++;
498     }
499
500   for (int i = first; i <= last; i++)
501     {
502       Offset o (encompass_offset (encompass_arr[i]));
503       offset_arr.push (o - origin);
504     }
505
506   offset_arr.push (Offset (spanner_length ()+  dx_f_drul_[RIGHT],
507                            dy_f_drul_[RIGHT]));
508
509   return offset_arr;
510 }
511
512
513 Array<Rod>
514 Slur::get_rods () const
515 {
516   Array<Rod> a;
517   Rod r;
518   r.item_l_drul_ = spanned_drul_;
519   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
520
521   a.push (r);
522   return a;
523 }
524
525