]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
patch::: 1.3.11.hwn1
[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 "group-interface.hh"
17 #include "slur.hh"
18 #include "lookup.hh"
19 #include "paper-def.hh"
20 #include "note-column.hh"
21 #include "stem.hh"
22 #include "paper-column.hh"
23 #include "molecule.hh"
24 #include "debug.hh"
25 #include "box.hh"
26 #include "bezier.hh"
27 #include "main.hh"
28 #include "cross-staff.hh"
29 #include "group-interface.hh"
30
31 Slur::Slur ()
32 {
33   set_elt_property ("note-columns", SCM_EOL);
34 }
35
36 void
37 Slur::add_column (Note_column*n)
38 {
39   if (!gh_pair_p (n->get_elt_property ("note-heads")))
40     warning (_ ("Putting slur over rest.  Ignoring."));
41   else
42     {
43       Group_interface gi (this, "note-columns");
44       gi.add_element (n);
45       add_dependency (n);
46     }
47 }
48
49 Direction
50 Slur::get_default_dir () const
51 {
52   Link_array<Note_column> encompass_arr =
53     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
54   
55   Direction d = DOWN;
56   for (int i=0; i < encompass_arr.size (); i ++) 
57     {
58       if (encompass_arr[i]->dir () < 0) 
59         {
60           d = UP;
61           break;
62         }
63     }
64   return d;
65 }
66
67 void
68 Slur::do_add_processing ()
69 {
70   Link_array<Note_column> encompass_arr =
71     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
72   set_bounds (LEFT, encompass_arr[0]);    
73   if (encompass_arr.size () > 1)
74     set_bounds (RIGHT, encompass_arr.top ());
75 }
76
77 void
78 Slur::do_pre_processing ()
79 {
80   // don't set directions
81 }
82
83
84 static int 
85 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
86 {
87   return Item::left_right_compare (n1, n2);
88 }
89
90
91 Offset
92 Slur::encompass_offset (Note_column const* col) const
93 {
94   Offset o;
95   Stem* stem_l = col->stem_l ();
96   if (!stem_l)
97     {
98       warning (_ ("Slur over rest?"));
99       o[X_AXIS] = col->hpos_f ();
100       o[Y_AXIS] = col->extent (Y_AXIS)[get_direction ()];
101       return o;  
102     }
103   
104   o[X_AXIS] = stem_l->hpos_f ();
105
106   /*
107     Simply set x to middle of notehead
108    */
109
110   o[X_AXIS] -= 0.5 * stem_l->get_direction () * col->extent (X_AXIS).length ();
111
112   if ((stem_l->get_direction () == get_direction ())
113       && !stem_l->extent (Y_AXIS).empty_b ())
114     {
115       o[Y_AXIS] = stem_l->extent (Y_AXIS)[get_direction ()];
116     }
117   else
118     {
119       o[Y_AXIS] = col->extent (Y_AXIS)[get_direction ()];
120     }
121
122   /*
123    leave a gap: slur mustn't touch head/stem
124    */
125   o[Y_AXIS] += get_direction () * paper_l ()->get_var ("slur_y_free");
126   o[Y_AXIS] += calc_interstaff_dist (stem_l, this);
127   return o;
128 }
129
130 /*
131   ARGRARGRARGRARGAR!
132
133   Fixme
134  */
135 void
136 Slur::do_post_processing ()
137 {
138     Link_array<Note_column> encompass_arr =
139     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
140
141   encompass_arr.sort (Note_column_compare);
142   if (!get_direction ())
143     set_direction (get_default_dir ());
144
145   /* 
146    Slur and tie placement [OSU]
147
148    Slurs:
149    * x = centre of head - d * x_gap_f
150
151    TODO:
152    * y = length < 5ss : horizontal tangent + d * 0.25 ss
153      y = length >= 5ss : y next interline - d * 0.25 ss
154    */
155
156   Real interline_f = paper_l ()->get_var ("interline");
157   Real internote_f = interline_f / 2;
158
159   Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
160   Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
161
162   Drul_array<Note_column*> note_column_drul;
163   note_column_drul[LEFT] = encompass_arr[0];
164   note_column_drul[RIGHT] = encompass_arr.top ();
165
166   bool fix_broken_b = false;
167   Direction d = LEFT;
168   do 
169     {
170       dx_f_drul_[d] = dy_f_drul_[d] = 0;
171       if ((note_column_drul[d] == spanned_drul_[d])
172           && note_column_drul[d]->first_head ()
173           && (note_column_drul[d]->stem_l ()))
174         {
175           Stem* stem_l = note_column_drul[d]->stem_l ();
176           /*
177             side directly attached to note head;
178             no beam getting in the way
179           */
180           if ((stem_l->extent (Y_AXIS).empty_b ()
181                || !((stem_l->get_direction () == get_direction ()) && (get_direction () != d)))
182               && !((get_direction () == stem_l->get_direction ())
183                    && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
184             {
185               dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
186               dx_f_drul_[d] -= d * x_gap_f;
187
188               if (stem_l->get_direction () != get_direction ())
189                 {
190                   dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[get_direction ()];
191                 }
192               else
193                 {
194                   dy_f_drul_[d] = stem_l->chord_start_f ()
195                     + get_direction () * internote_f;
196                 }
197               dy_f_drul_[d] += get_direction () * y_gap_f;
198             }
199           /*
200             side attached to (visible) stem
201           */
202           else
203             {
204               dx_f_drul_[d] = stem_l->hpos_f ()
205                 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
206               /*
207                 side attached to beamed stem
208                */
209               if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
210                 {
211                   dy_f_drul_[d] = stem_l->extent (Y_AXIS)[get_direction ()];
212                   dy_f_drul_[d] += get_direction () * 2 * y_gap_f;
213                 }
214               /*
215                 side attached to notehead, with stem getting in the way
216                */
217               else
218                 {
219                   dx_f_drul_[d] -= d * x_gap_f;
220                   
221                   dy_f_drul_[d] = stem_l->chord_start_f ()
222                     + get_direction () * internote_f;
223                   dy_f_drul_[d] += get_direction () * y_gap_f;
224                 }
225             }
226         }
227       /*
228         loose end
229       */
230       else
231         {
232           dx_f_drul_[d] = get_broken_left_end_align ();
233                 
234           /*
235             broken: should get y from other piece, so that slur
236             continues up/down trend
237
238             for now: be horizontal..
239           */
240           fix_broken_b = true;
241         }
242     }
243   while (flip (&d) != LEFT);
244
245   int cross_count =  cross_staff_count ();
246   bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
247
248   Drul_array<Offset> info_drul;
249   Drul_array<Real> interstaff_interval;
250
251   do
252     {
253       info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
254       interstaff_interval[d] = calc_interstaff_dist (encompass_arr.boundary (d,0),
255                                                      this);
256     }
257   while (flip (&d) != LEFT);
258   
259   Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
260
261   if (fix_broken_b)
262     {
263       Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
264         RIGHT : LEFT;
265       dy_f_drul_[d] = info_drul[d][Y_AXIS];
266       if (!interstaff_b)
267         {
268           dy_f_drul_[d] -= interstaff_interval[d];
269           if (cross_count)      // interstaff_i  ? 
270             {
271               dy_f_drul_[LEFT] += interstaff_interval[d];
272               dy_f_drul_[RIGHT] += interstaff_interval[d];
273             }
274         }
275     }
276         
277
278   /*
279     Now we've got a fine slur
280     Catch and correct some ugly cases
281    */
282   String infix = interstaff_b ? "interstaff_" : "";
283   Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
284   Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
285   Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
286   Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
287
288   if (!fix_broken_b)
289     dy_f_drul_[RIGHT] += interstaff_f;
290
291   Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
292   if (!fix_broken_b)
293     dy_f -= interstaff_f;
294   Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
295
296   /*
297     Avoid too steep slurs.
298    */
299   Real slope_ratio_f = abs (dy_f / dx_f);
300   if (slope_ratio_f > slope_damp_f)
301     {
302       Direction d = (Direction)(- get_direction () * (sign (dy_f)));
303       if (!d)
304         d = LEFT;
305       Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
306       /*
307         must never change sign of dy
308        */
309       damp_f = damp_f <? abs (dy_f);
310       dy_f_drul_[d] += get_direction () * damp_f;
311     }
312
313   /*
314    Avoid too high slurs 
315
316    Wierd slurs may look a lot better after they have been
317    adjusted a bit.
318    So, we'll do this in 3 steps
319    */
320   for (int i = 0; i < 3; i++)
321     {
322       Drul_array<Interval> curve_xy_drul = curve_extent_drul ();
323       Real height_f = curve_xy_drul[Y].length ();
324       Real width_f = curve_xy_drul[X].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)(- get_direction () * (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] += get_direction () * damp_f;
344               dy_f_drul_[d] += get_direction () * 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] += get_direction () * 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 () == get_direction ()
372           && abs (nc->stem_l ()->extent (Y_AXIS)[get_direction ()]
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)[get_direction ()]
383             + interstaff_interval[d]
384             + get_direction () * 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