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