]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.1.57
[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 "p-col.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->get_elt_property (transparent_scm_sym) != SCM_BOOL_F)
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           dx_f_drul_[d] -= d * x_gap_f;
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   if (fix_broken_b)
202     do {
203       if (dy_f_drul_[d])
204         dy_f_drul_[-d] = dy_f_drul_[d];
205     }
206     while (flip (&d) != LEFT);
207         
208
209   /*
210     Now we've got a fine slur
211     Catch and correct some ugly cases
212    */
213
214   Real dx_f = do_width ().length () + dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
215   Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
216   Real height_f = do_height ().length ();
217
218   Real height_damp_f = paper_l ()->get_var ("slur_height_damping");
219   Real slope_damp_f = paper_l ()->get_var ("slur_slope_damping");
220   Real ratio_f;
221
222
223   /*
224     Avoid too steep slurs.
225    */
226   ratio_f = abs (dy_f / dx_f);
227   if (ratio_f > slope_damp_f)
228     {
229       Direction d = (Direction)(- dir_ * (sign (dy_f)));
230       if (!d)
231         d = LEFT;
232       dy_f_drul_[d] += dir_ * (ratio_f - slope_damp_f) * dx_f;
233     }
234
235   /*
236    Avoid too high slurs 
237    */
238   ratio_f = abs (height_f / dx_f);
239   if (ratio_f > height_damp_f)
240     {
241       Direction d = (Direction)(- dir_ * (sign (dy_f)));
242       if (!d)
243         d = LEFT;
244       dy_f_drul_[d] += dir_ * height_f * height_damp_f;
245       /*
246         if y positions at same height, correct both ends
247        */
248       if (abs (dy_f / dx_f ) < slope_damp_f)
249         {
250           dy_f_drul_[-d] += dir_ * height_f * height_damp_f;
251         }
252     }
253
254   /*
255     If, after correcting, we're close to stem-end...
256   */
257   Real snap_f = paper_l ()->get_var ("slur_snap_to_stem");
258   do
259     {
260       if ((note_column_drul[d] == spanned_drul_[d])
261           && (note_column_drul[d]->stem_l_)
262           && (note_column_drul[d]->stem_l_->dir_ == dir_)
263           && (abs (note_column_drul[d]->stem_l_->extent (Y_AXIS)[dir_]
264                    - dy_f_drul_[d]) <= snap_f))
265         {
266           /*
267             attach to stem-end
268           */
269           Stem* stem_l = note_column_drul[d]->stem_l_;
270           dx_f_drul_[d] = stem_l->hpos_f ()
271             - spanned_drul_[d]->absolute_coordinate (X_AXIS);
272           dy_f_drul_[d] = stem_l->extent (Y_AXIS)[dir_];
273           dy_f_drul_[d] += dir_ * 2 * y_gap_f;
274         }
275     }
276   while (flip (&d) != LEFT);
277 }
278
279 Array<Offset>
280 Slur::get_encompass_offset_arr () const
281 {
282   Array<Offset> offset_arr;
283   Offset origin (absolute_coordinate (X_AXIS), 0);
284
285   int first = 1;
286   int last = encompass_arr_.size () - 2;
287
288   /*
289     left is broken edge
290   */
291   if (encompass_arr_[0] != spanned_drul_[LEFT])
292     {
293       first--;
294     }
295   Encompass_info left_info (encompass_arr_[0], dir_, this);
296   offset_arr.push (Offset (0, left_info.interstaff_f_));
297
298   /*
299     right is broken edge
300   */
301   if (encompass_arr_.top () != spanned_drul_[RIGHT])
302     {
303       last++;
304     }
305
306   for (int i = first; i <= last; i++)
307     {
308       Encompass_info info (encompass_arr_[i], dir_, this);
309       offset_arr.push (info.o_ - origin);
310     }
311
312   offset_arr.push (Offset (do_width ().length (), 0));
313
314 #if 1
315   offset_arr[0] += Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
316   offset_arr.top () += Offset (dx_f_drul_[RIGHT], dy_f_drul_[RIGHT]);
317 #else
318   /*
319     check non-disturbed slur
320     FIXME: ends off by a tiny bit!!
321   */
322   offset_arr[0] += Offset (0, dy_f_drul_[LEFT]);
323   offset_arr.top () += Offset (0, dy_f_drul_[RIGHT]);
324 #endif
325   
326   return offset_arr;
327 }
328
329
330 Array<Rod>
331 Slur::get_rods () const
332 {
333   Array<Rod> a;
334   Rod r;
335   r.item_l_drul_ = spanned_drul_;
336   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
337
338   a.push (r);
339   return a;
340 }
341