]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
patch::: 1.3.16.jcn1
[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
138   if (!directional_element (this).get ())
139     directional_element (this).set (get_default_dir ());
140
141   /* 
142    Slur and tie placement [OSU]
143
144    Slurs:
145    * x = centre of head - d * x_gap_f
146
147    TODO:
148    * y = length < 5ss : horizontal tangent + d * 0.25 ss
149      y = length >= 5ss : y next interline - d * 0.25 ss
150    */
151
152   Real staff_space = paper_l ()->get_var ("interline");
153   Real half_staff_space = staff_space / 2;
154
155   Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
156   Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
157
158   Drul_array<Note_column*> note_column_drul;
159   note_column_drul[LEFT] = encompass_arr[0];
160   note_column_drul[RIGHT] = encompass_arr.top ();
161
162   bool fix_broken_b = false;
163
164   Direction my_dir = directional_element (this).get ();
165   
166   Direction d = LEFT;
167   do 
168     {
169       dx_f_drul_[d] = 0;
170       dy_f_drul_[d] = 0;
171       
172       if ((note_column_drul[d] == spanned_drul_[d])
173           && note_column_drul[d]->first_head ()
174           && (note_column_drul[d]->stem_l ()))
175         {
176           Stem* stem_l = note_column_drul[d]->stem_l ();
177           /*
178             side directly attached to note head;
179             no beam getting in the way
180           */
181           if ((stem_l->extent (Y_AXIS).empty_b ()
182                || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
183               && !((my_dir == stem_l->get_direction ())
184                    && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
185             {
186               dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
187               dx_f_drul_[d] -= d * x_gap_f;
188
189               if (stem_l->get_direction () != my_dir)
190                 {
191                   dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
192                 }
193               else
194                 {
195                   dy_f_drul_[d] = stem_l->chord_start_f ()
196                     + my_dir * half_staff_space;
197                 }
198               dy_f_drul_[d] += my_dir * y_gap_f;
199             }
200           /*
201             side attached to (visible) stem
202           */
203           else
204             {
205               dx_f_drul_[d] = stem_l->hpos_f ()
206                 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
207               /*
208                 side attached to beamed stem
209                */
210               if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
211                 {
212                   dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
213                   dy_f_drul_[d] += my_dir * 2 * y_gap_f;
214                 }
215               /*
216                 side attached to notehead, with stem getting in the way
217                */
218               else
219                 {
220                   dx_f_drul_[d] -= d * x_gap_f;
221                   
222                   dy_f_drul_[d] = stem_l->chord_start_f ()
223                     + my_dir * half_staff_space;
224                   dy_f_drul_[d] += my_dir * y_gap_f;
225                 }
226             }
227         }
228       /*
229         loose end
230       */
231       else
232         {
233           dx_f_drul_[d] = get_broken_left_end_align ();
234                 
235           /*
236             broken: should get y from other piece, so that slur
237             continues up/down trend
238
239             for now: be horizontal..
240           */
241           fix_broken_b = true;
242         }
243     }
244   while (flip (&d) != LEFT);
245
246   int cross_count =  cross_staff_count ();
247   bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
248
249   Drul_array<Offset> info_drul;
250   Drul_array<Real> interstaff_interval;
251
252   do
253     {
254       info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
255       interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
256                                                      this);
257     }
258   while (flip (&d) != LEFT);
259   
260   Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
261
262   if (fix_broken_b)
263     {
264       Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
265         RIGHT : LEFT;
266       dy_f_drul_[d] = info_drul[d][Y_AXIS];
267       if (!interstaff_b)
268         {
269           dy_f_drul_[d] -= interstaff_interval[d];
270           if (cross_count)      // interstaff_i  ? 
271             {
272               dy_f_drul_[LEFT] += interstaff_interval[d];
273               dy_f_drul_[RIGHT] += interstaff_interval[d];
274             }
275         }
276     }
277         
278
279   /*
280     Now we've got a fine slur
281     Catch and correct some ugly cases
282    */
283   String infix = interstaff_b ? "interstaff_" : "";
284   Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
285   Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
286   Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
287   Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
288
289   if (!fix_broken_b)
290     dy_f_drul_[RIGHT] += interstaff_f;
291
292   Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
293   if (!fix_broken_b)
294     dy_f -= interstaff_f;
295   Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
296
297   /*
298     Avoid too steep slurs.
299    */
300   Real slope_ratio_f = abs (dy_f / dx_f);
301   if (slope_ratio_f > slope_damp_f)
302     {
303       Direction d = (Direction)(- my_dir * (sign (dy_f)));
304       if (!d)
305         d = LEFT;
306       Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
307       /*
308         must never change sign of dy
309        */
310       damp_f = damp_f <? abs (dy_f);
311       dy_f_drul_[d] += my_dir * damp_f;
312     }
313
314   /*
315    Avoid too high slurs 
316
317    Wierd slurs may look a lot better after they have been
318    adjusted a bit.
319    So, we'll do this in 3 steps
320    */
321   for (int i = 0; i < 3; i++)
322     {
323       Real height_f = curve_extent (Y_AXIS).length ();
324       Real width_f = curve_extent (X_AXIS).length ();
325       
326       dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
327       if (!fix_broken_b)
328         dy_f -= interstaff_f;
329
330       Real height_ratio_f = abs (height_f / width_f);
331       if (height_ratio_f > height_damp_f)
332         {
333           Direction d = (Direction)(- my_dir * (sign (dy_f)));
334           if (!d)
335             d = LEFT;
336           /* take third step */
337           Real damp_f = (height_ratio_f - height_damp_f) * width_f / 3;
338           /*
339             if y positions at about the same height, correct both ends
340           */
341           if (abs (dy_f / dx_f ) < slope_damp_f)
342             {
343               dy_f_drul_[-d] += my_dir * damp_f;
344               dy_f_drul_[d] += my_dir * damp_f;
345             }
346           /*
347             don't change slope too much, would have been catched by slope damping
348           */
349           else
350             {
351               damp_f = damp_f <? abs (dy_f/2);
352               dy_f_drul_[d] += my_dir * damp_f;
353             }
354         }
355     }
356
357   /*
358     If, after correcting, we're close to stem-end...
359   */
360   Drul_array<Real> snapy_f_drul;
361   snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
362   Drul_array<Real> snapx_f_drul;
363   snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
364   Drul_array<bool> snapped_b_drul;
365   snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
366   do
367     {
368       Note_column * nc = note_column_drul[d];
369       if (nc == spanned_drul_[d]
370           && nc->stem_l ()
371           && nc->stem_l ()->get_direction () == my_dir
372           && abs (nc->stem_l ()->extent (Y_AXIS)[my_dir]
373                   - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
374               <= snap_f)
375         {
376           /*
377             prepare to attach to stem-end
378           */
379           snapx_f_drul[d] = nc->stem_l ()->hpos_f ()
380             - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
381
382           snapy_f_drul[d] = nc->stem_l ()->extent (Y_AXIS)[my_dir]
383             + interstaff_interval[d]
384             + my_dir * 2 * y_gap_f;
385           
386           snapped_b_drul[d] = true;
387         }
388     }
389   while (flip (&d) != LEFT);
390
391   /*
392     only use snapped positions if sign (dy) will not change
393     and dy doesn't change too much
394     */
395   if (!fix_broken_b)
396     dy_f += interstaff_f;
397
398
399   /*
400     (sigh)
401
402     More refactoring could be done.
403    */
404   Real maxsnap = abs (dy_f * snap_max_dy_f);
405   if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
406       && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
407       && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
408                     < maxsnap)))
409     {
410       dy_f_drul_ = snapy_f_drul;
411       dx_f_drul_ = snapx_f_drul;
412     }
413   else
414     do
415       {
416         Direction od = (Direction)-d;
417         if (snapped_b_drul[d]
418             && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
419             && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od]  - d * dy_f)
420                           < maxsnap)))
421           {
422             dy_f_drul_[d] = snapy_f_drul[d];
423             dx_f_drul_[d] = snapx_f_drul[d];
424           }
425       }
426     while (flip (&d) != LEFT);
427 }
428
429
430 int
431 Slur::cross_staff_count ()const
432 {
433   Link_array<Note_column> encompass_arr =
434     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
435
436   int k=0;
437
438   for (int i = 0; i < encompass_arr.size (); i++)
439     {
440       if (calc_interstaff_dist (encompass_arr[i], this))
441         k++;
442     }
443   return k;
444 }
445
446
447 Array<Offset>
448 Slur::get_encompass_offset_arr () const
449 {
450   Link_array<Note_column> encompass_arr =
451     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
452   
453   Array<Offset> offset_arr;
454 #if 0
455   /*
456     check non-disturbed slur
457     FIXME: x of ends off by a tiny bit!!
458   */
459   offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
460   offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
461   return offset_arr;
462 #endif
463   
464   Offset origin (relative_coordinate (0, X_AXIS), 0);
465
466   int first = 1;
467   int last = encompass_arr.size () - 2;
468
469   offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
470
471   /*
472     left is broken edge
473   */
474
475   int cross_count  = cross_staff_count ();
476   bool cross_b = cross_count && cross_count < encompass_arr.size ();
477   if (encompass_arr[0] != spanned_drul_[LEFT])
478     {
479       first--;
480       Real is   = calc_interstaff_dist (encompass_arr[0], this);
481       if (cross_b)
482         offset_arr[0][Y_AXIS] += is;
483     }
484
485   /*
486     right is broken edge
487   */
488   if (encompass_arr.top () != spanned_drul_[RIGHT])
489     {
490       last++;
491     }
492
493   for (int i = first; i <= last; i++)
494     {
495       Offset o (encompass_offset (encompass_arr[i]));
496       offset_arr.push (o - origin);
497     }
498
499   offset_arr.push (Offset (spanner_length ()+  dx_f_drul_[RIGHT],
500                            dy_f_drul_[RIGHT]));
501
502   return offset_arr;
503 }
504
505
506 Array<Rod>
507 Slur::get_rods () const
508 {
509   Array<Rod> a;
510   Rod r;
511   r.item_l_drul_ = spanned_drul_;
512   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
513
514   a.push (r);
515   return a;
516 }
517
518