]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.1.38
[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, 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 /*
11   [TODO]
12     * URG: share code with tie
13     * begin and end should be treated as a Script.
14     * damping
15     * slur from notehead to stemend: c''()b''
16  */
17
18 #include "slur.hh"
19 #include "scalar.hh"
20 #include "lookup.hh"
21 #include "paper-def.hh"
22 #include "note-column.hh"
23 #include "stem.hh"
24 #include "p-col.hh"
25 #include "molecule.hh"
26 #include "debug.hh"
27 #include "box.hh"
28 #include "bezier.hh"
29 #include "encompass-info.hh"
30 #include "main.hh"
31
32
33 Slur::Slur ()
34 {
35 }
36
37 void
38 Slur::add_column (Note_column*n)
39 {
40   if (!n->head_l_arr_.size ())
41     warning (_ ("Putting slur over rest."));
42   encompass_arr_.push (n);
43   //  n->stem_l_->slur_l_ = this;
44   add_dependency (n);
45 }
46
47 void
48 Slur::set_default_dir ()
49 {
50   dir_ = DOWN;
51   for (int i=0; i < encompass_arr_.size (); i ++) 
52     {
53       if (encompass_arr_[i]->dir () < 0) 
54         {
55           dir_ = UP;
56           break;
57         }
58     }
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 static bool
95 broken_edge_b (Slur*s, Drul_array<Note_column*>& extrema, Direction dir)
96 {
97   return extrema[dir] != s->spanned_drul_[dir];
98 }
99
100 static bool
101 normal_edge_b (Slur*s, Drul_array<Note_column*>& extrema, Direction dir)
102 {
103   Note_column *n = extrema[dir];
104   return !broken_edge_b (s, extrema, dir)
105     && n->stem_l_
106     && n->stem_l_->get_elt_property (transparent_scm_sym) == SCM_BOOL_F
107     && n->head_l_arr_.size ();
108 }
109
110 void
111 Slur::do_post_processing ()
112 {
113   encompass_arr_.sort (Note_column_compare);
114   if (!dir_)
115     set_default_dir ();
116
117   Real interline_f = paper_l ()->get_realvar (interline_scm_sym);
118   Real internote_f = interline_f / 2;
119   // URG
120   Real notewidth_f = paper_l ()->note_width () * 0.8;
121
122   /* 
123    [OSU]: slur and tie placement
124
125    slurs:
126    * x = centre of head (upside-down: inner raakpunt stem) - d * gap
127
128    * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
129      y = length >= 5ss : y next interline - d * 0.25 ss
130      --> height <= 5 length ?? we use <= 3 length, now...
131    */
132   
133   Real gap_f = paper_l ()->get_var ("slur_x_gap");
134
135   Drul_array<Note_column*> extrema;
136   extrema[LEFT] = encompass_arr_[0];
137   extrema[RIGHT] = encompass_arr_.top ();
138
139   Direction d=LEFT;
140  
141   do 
142     {
143       if (broken_edge_b (this, extrema, d))
144         {
145           // ugh -- check if needed
146           dx_f_drul_[d] = -d 
147             *(spanned_drul_[d]->extent (X_AXIS).length () - 0.5 * notewidth_f);
148
149           // prebreak
150           if (d == RIGHT)
151             {
152               dx_f_drul_[LEFT] = spanned_drul_[LEFT]->extent (X_AXIS).length ();
153               
154               // urg -- check if needed
155               if (encompass_arr_.size () > 1)
156                 dx_f_drul_[RIGHT] += notewidth_f;
157             }
158         }
159       /*
160         normal slur
161        */
162       else if (normal_edge_b (this, extrema, d))
163         {
164           Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
165           dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_-> extent (Y_AXIS)[dir_]);
166           dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
167           if (dir_ == extrema[d]->stem_l_->dir_)
168             {
169               if (dir_ == d)
170                 dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
171               else
172                 dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
173             }
174         }
175         else 
176           {
177             Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
178             dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
179                                        [dir_]) * internote_f;
180             dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
181         }
182         dy_f_drul_[d] += dir_ * interline_f;
183         if (extrema[d]->stem_l_ && (dir_ == extrema[d]->stem_l_->dir_))
184           dy_f_drul_[d] -= dir_ * internote_f;
185       }
186   while (flip(&d) != LEFT);
187
188   // now that both are set, do dependent
189   do 
190     {
191       if (broken_edge_b (this, extrema, d))
192         {
193           Direction u = d;
194           flip(&u);
195
196           // postbreak
197           if (d == LEFT)
198             dy_f_drul_[u] += dir_ * internote_f;
199
200           dy_f_drul_[d] = dy_f_drul_[u];
201         }
202      }
203   while (flip(&d) != LEFT);
204
205   /*
206     Slur should follow line of music
207    */
208   if (normal_edge_b (this, extrema, LEFT)
209       && normal_edge_b (this, extrema, RIGHT)
210       && (extrema[LEFT]->stem_l_ != extrema[RIGHT]->stem_l_))
211     {
212       Real note_dy = extrema[RIGHT]->stem_l_->head_positions ()[dir_]
213         - extrema[LEFT]->stem_l_->head_positions ()[dir_];
214       Real dy = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
215       /*
216         Should we always follow note-heads, (like a tie)?
217         For now, only if the note_dy != slur_dy, we'll do
218         slur_dy := note_dy * factor.
219       */
220       if (sign (dy) != sign (note_dy))
221         {
222           Real damp_f = paper_l ()->get_var ("slur_slope_follow_music_factor");
223           Real realdy = note_dy * damp_f;
224           Direction adjust_dir = (Direction)(- dir_ * sign (realdy));
225           if (!adjust_dir)
226             adjust_dir = -dir_;
227           /*
228             adjust only if no beam gets in the way
229            */
230           if (!extrema[adjust_dir]->stem_l_->beam_l_
231               || (adjust_dir == extrema[adjust_dir]->stem_l_->dir_)
232               || (extrema[adjust_dir]->stem_l_->beams_i_drul_[-adjust_dir] < 1))
233             {
234               dy_f_drul_[adjust_dir] = dy_f_drul_[-adjust_dir]
235                 + 2 * adjust_dir * realdy;
236               Real dx = notewidth_f / 2;
237               if (adjust_dir != extrema[adjust_dir]->stem_l_->dir_)
238                 dx /= 2;
239               dx_f_drul_[adjust_dir] -= adjust_dir * dx;
240             }
241         }
242     }
243
244   /*
245     Avoid too steep slurs.
246    */
247   Real damp_f = paper_l ()->get_var ("slur_slope_damping");
248   Offset d_off = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
249     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
250   d_off[X_AXIS] += extent (X_AXIS).length ();
251
252   Real ratio_f = abs (d_off[Y_AXIS] / d_off[X_AXIS]);
253   if (ratio_f > damp_f)
254     dy_f_drul_[(Direction)(- dir_ * sign (d_off[Y_AXIS]))] +=
255       dir_ * (ratio_f - damp_f) * d_off[X_AXIS];
256 }
257
258 Array<Offset>
259 Slur::get_encompass_offset_arr () const
260 {
261   Real notewidth = paper_l ()->note_width () * 0.8;
262   Real gap = paper_l ()->get_var ("slur_x_gap");
263
264   Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
265   left[X_AXIS] += encompass_arr_[0]->stem_l_->hpos_f ();
266   Real internote = encompass_arr_[0]->stem_l_->staff_line_leading_f ()/2.0;
267
268   /*
269     <URG>
270     i don't understand these two, but *must* for symmetry 
271     look at encompass array: 
272        lilypond -D input/test/slur-symmetry*.ly
273        lilypond -D input/test/sleur.ly
274
275     do_post_processing should have calculated these into
276     dx_f_drul_[], no??
277
278    */
279
280   if (dir_ != encompass_arr_[0]->stem_l_->dir_)
281     left[X_AXIS] += - 0.5 * notewidth * encompass_arr_[0]->stem_l_->dir_
282       + gap;
283   else if (encompass_arr_[0]->stem_l_->dir_ == UP)
284     left[X_AXIS] -= notewidth;
285
286   if ((dir_ == encompass_arr_[0]->stem_l_->dir_) 
287     && (encompass_arr_[0]->stem_l_->dir_ == DOWN))
288     left[Y_AXIS] -= internote * encompass_arr_[0]->stem_l_->dir_;
289   /* </URG> */
290
291   Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
292     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
293   d[X_AXIS] += extent (X_AXIS).length ();
294
295   int first = 1;
296   int last = encompass_arr_.size () - 1;
297
298   // prebreak
299   if (encompass_arr_.top () != spanned_drul_[RIGHT])
300     last++;
301
302   // postbreak
303   if (encompass_arr_[0] != spanned_drul_[LEFT])
304     first--;
305
306   Array<Offset> notes;
307   notes.push (Offset (0,0));
308
309   for (int i = first; i < last; i++)
310     {
311       Encompass_info info (encompass_arr_[i], dir_, this);
312       notes.push (info.o_ - left);
313     }
314   Encompass_info info (encompass_arr_.top (), dir_, this);
315   Real inter_staff = info.interstaff_f_;
316   
317   d[Y_AXIS] += inter_staff;
318
319   // prebreak
320   if (inter_staff && (encompass_arr_.top () != spanned_drul_[RIGHT]))
321     {
322       Encompass_info info (encompass_arr_[encompass_arr_.size () - 1], dir_, this);
323       d[Y_AXIS] -= info.o_[Y_AXIS] - inter_staff;
324     }
325
326   notes.push (d);
327
328   return notes;
329 }
330
331
332 Array<Rod>
333 Slur::get_rods () const
334 {
335   Array<Rod> a;
336   Rod r;
337   r.item_l_drul_ = spanned_drul_;
338   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
339
340   a.push (r);
341   return a;
342 }
343