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