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