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