]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
patch::: 1.3.9.hwn2
[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 "slur.hh"
17
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
30 Slur::Slur ()
31 {
32 }
33
34 void
35 Slur::add_column (Note_column*n)
36 {
37   if (!n->head_l_arr_.size ())
38     warning (_ ("Putting slur over rest.  Ignoring."));
39   else
40     {
41       encompass_arr_.push (n);
42       add_dependency (n);
43     }
44 }
45
46 Direction
47 Slur::get_default_dir () const
48 {
49   Direction d = DOWN;
50   for (int i=0; i < encompass_arr_.size (); i ++) 
51     {
52       if (encompass_arr_[i]->dir () < 0) 
53         {
54           d = UP;
55           break;
56         }
57     }
58   return d;
59 }
60
61 void
62 Slur::do_add_processing ()
63 {
64   set_bounds (LEFT, encompass_arr_[0]);    
65   if (encompass_arr_.size () > 1)
66     set_bounds (RIGHT, encompass_arr_.top ());
67 }
68
69 void
70 Slur::do_pre_processing ()
71 {
72   // don't set directions
73 }
74
75 void
76 Slur::do_substitute_element_pointer (Score_element*o, Score_element*n)
77 {
78   int i;
79   while ((i = encompass_arr_.find_i (dynamic_cast<Note_column *> (o))) >=0) 
80     {
81       if (n)
82         encompass_arr_[i] = dynamic_cast<Note_column *> (n);
83       else
84         encompass_arr_.del (i);
85     }
86 }
87
88 static int 
89 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
90 {
91   return Item::left_right_compare (n1, n2);
92 }
93
94
95 Offset
96 Slur::encompass_offset (Note_column const* col) const
97 {
98   Offset o;
99   Stem* stem_l = col->stem_l_;
100   if (!stem_l)
101     {
102       warning (_ ("Slur over rest?"));
103       o[X_AXIS] = col->hpos_f ();
104       o[Y_AXIS] = col->extent (Y_AXIS)[get_direction ()];
105       return o;  
106     }
107   
108   o[X_AXIS] = stem_l->hpos_f ();
109
110   /*
111     Simply set x to middle of notehead
112    */
113
114   o[X_AXIS] -= 0.5 * stem_l->get_direction () * col->extent (X_AXIS).length ();
115
116   if ((stem_l->get_direction () == get_direction ())
117       && !stem_l->extent (Y_AXIS).empty_b ())
118     {
119       o[Y_AXIS] = stem_l->extent (Y_AXIS)[get_direction ()];
120     }
121   else
122     {
123       o[Y_AXIS] = col->extent (Y_AXIS)[get_direction ()];
124     }
125
126   /*
127    leave a gap: slur mustn't touch head/stem
128    */
129   o[Y_AXIS] += get_direction () * paper_l ()->get_var ("slur_y_free");
130   o[Y_AXIS] += calc_interstaff_dist (stem_l, this);
131   return o;
132 }
133
134 /*
135   ARGRARGRARGRARGAR!
136
137   Fixme
138  */
139 void
140 Slur::do_post_processing ()
141 {
142   encompass_arr_.sort (Note_column_compare);
143   if (!get_direction ())
144     set_direction (get_default_dir ());
145
146   /* 
147    Slur and tie placement [OSU]
148
149    Slurs:
150    * x = centre of head - d * x_gap_f
151
152    TODO:
153    * y = length < 5ss : horizontal tangent + d * 0.25 ss
154      y = length >= 5ss : y next interline - d * 0.25 ss
155    */
156
157   Real interline_f = paper_l ()->get_var ("interline");
158   Real internote_f = interline_f / 2;
159
160   Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
161   Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
162
163   Drul_array<Note_column*> note_column_drul;
164   note_column_drul[LEFT] = encompass_arr_[0];
165   note_column_drul[RIGHT] = encompass_arr_.top ();
166
167   bool fix_broken_b = false;
168   Direction d = LEFT;
169   do 
170     {
171       dx_f_drul_[d] = dy_f_drul_[d] = 0;
172       if ((note_column_drul[d] == spanned_drul_[d])
173           && note_column_drul[d]->head_l_arr_.size ()
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 () == get_direction ()) && (get_direction () != d)))
183               && !((get_direction () == stem_l->get_direction ())
184                    && stem_l->beam_l () && (stem_l->beams_i_drul_[-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 () != get_direction ())
190                 {
191                   dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[get_direction ()];
192                 }
193               else
194                 {
195                   dy_f_drul_[d] = stem_l->chord_start_f ()
196                     + get_direction () * internote_f;
197                 }
198               dy_f_drul_[d] += get_direction () * 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->beams_i_drul_[-d] >= 1))
211                 {
212                   dy_f_drul_[d] = stem_l->extent (Y_AXIS)[get_direction ()];
213                   dy_f_drul_[d] += get_direction () * 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                     + get_direction () * internote_f;
224                   dy_f_drul_[d] += get_direction () * 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)(- get_direction () * (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] += get_direction () * 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       Drul_array<Interval> curve_xy_drul = curve_extent_drul ();
324       Real height_f = curve_xy_drul[Y].length ();
325       Real width_f = curve_xy_drul[X].length ();
326       
327       dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
328       if (!fix_broken_b)
329         dy_f -= interstaff_f;
330
331       Real height_ratio_f = abs (height_f / width_f);
332       if (height_ratio_f > height_damp_f)
333         {
334           Direction d = (Direction)(- get_direction () * (sign (dy_f)));
335           if (!d)
336             d = LEFT;
337           /* take third step */
338           Real damp_f = (height_ratio_f - height_damp_f) * width_f / 3;
339           /*
340             if y positions at about the same height, correct both ends
341           */
342           if (abs (dy_f / dx_f ) < slope_damp_f)
343             {
344               dy_f_drul_[-d] += get_direction () * damp_f;
345               dy_f_drul_[d] += get_direction () * damp_f;
346             }
347           /*
348             don't change slope too much, would have been catched by slope damping
349           */
350           else
351             {
352               damp_f = damp_f <? abs (dy_f/2);
353               dy_f_drul_[d] += get_direction () * damp_f;
354             }
355         }
356     }
357
358   /*
359     If, after correcting, we're close to stem-end...
360   */
361   Drul_array<Real> snapy_f_drul;
362   snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
363   Drul_array<Real> snapx_f_drul;
364   snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
365   Drul_array<bool> snapped_b_drul;
366   snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
367   do
368     {
369       Note_column * nc = note_column_drul[d];
370       if (nc == spanned_drul_[d]
371           && nc->stem_l_
372           && nc->stem_l_->get_direction () == get_direction ()
373           && abs (nc->stem_l_->extent (Y_AXIS)[get_direction ()]
374                   - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
375               <= snap_f)
376         {
377           /*
378             prepare to attach to stem-end
379           */
380           snapx_f_drul[d] = nc->stem_l_->hpos_f ()
381             - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
382
383           snapy_f_drul[d] = nc->stem_l_->extent (Y_AXIS)[get_direction ()]
384             + interstaff_interval[d]
385             + get_direction () * 2 * y_gap_f;
386           
387           snapped_b_drul[d] = true;
388         }
389     }
390   while (flip (&d) != LEFT);
391
392   /*
393     only use snapped positions if sign (dy) will not change
394     and dy doesn't change too much
395     */
396   if (!fix_broken_b)
397     dy_f += interstaff_f;
398
399
400   /*
401     (sigh)
402
403     More refactoring could be done.
404    */
405   Real maxsnap = abs (dy_f * snap_max_dy_f);
406   if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
407       && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
408       && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
409                     < maxsnap)))
410     {
411       dy_f_drul_ = snapy_f_drul;
412       dx_f_drul_ = snapx_f_drul;
413     }
414   else
415     do
416       {
417         Direction od = (Direction)-d;
418         if (snapped_b_drul[d]
419             && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
420             && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od]  - d * dy_f)
421                           < maxsnap)))
422           {
423             dy_f_drul_[d] = snapy_f_drul[d];
424             dx_f_drul_[d] = snapx_f_drul[d];
425           }
426       }
427     while (flip (&d) != LEFT);
428 }
429
430
431 int
432 Slur::cross_staff_count ()const
433 {
434   int k=0;
435
436   for (int i = 0; i < encompass_arr_.size (); i++)
437     {
438       if (calc_interstaff_dist (encompass_arr_[i], this))
439         k++;
440     }
441   return k;
442 }
443
444
445 Array<Offset>
446 Slur::get_encompass_offset_arr () const
447 {
448   Array<Offset> offset_arr;
449 #if 0
450   /*
451     check non-disturbed slur
452     FIXME: x of ends off by a tiny bit!!
453   */
454   offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
455   offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
456   return offset_arr;
457 #endif
458   
459   Offset origin (relative_coordinate (0, X_AXIS), 0);
460
461   int first = 1;
462   int last = encompass_arr_.size () - 2;
463
464   offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
465
466   /*
467     left is broken edge
468   */
469
470   int cross_count  = cross_staff_count ();
471   bool cross_b = cross_count && cross_count < encompass_arr_.size ();
472   if (encompass_arr_[0] != spanned_drul_[LEFT])
473     {
474       first--;
475       Real is   = calc_interstaff_dist (encompass_arr_[0], this);
476       if (cross_b)
477         offset_arr[0][Y_AXIS] += is;
478     }
479
480   /*
481     right is broken edge
482   */
483   if (encompass_arr_.top () != spanned_drul_[RIGHT])
484     {
485       last++;
486     }
487
488   for (int i = first; i <= last; i++)
489     {
490       Offset o (encompass_offset (encompass_arr_[i]));
491       offset_arr.push (o - origin);
492     }
493
494   offset_arr.push (Offset (spanner_length ()+  dx_f_drul_[RIGHT],
495                            dy_f_drul_[RIGHT]));
496
497   return offset_arr;
498 }
499
500
501 Array<Rod>
502 Slur::get_rods () const
503 {
504   Array<Rod> a;
505   Rod r;
506   r.item_l_drul_ = spanned_drul_;
507   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
508
509   a.push (r);
510   return a;
511 }
512
513