]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.1.30
[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_dependency (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     set_default_dir ();
100
101   Real interline_f = paper ()->interline_f ();
102   Real internote_f = interline_f / 2;
103   // URG
104   Real notewidth_f = paper ()->note_width () * 0.8;
105
106   /* 
107    [OSU]: slur and tie placement
108
109    slurs:
110    * x = centre of head (upside-down: inner raakpunt stem) - d * gap
111
112    * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
113      y = length >= 5ss : y next interline - d * 0.25 ss
114      --> height <= 5 length ?? we use <= 3 length, now...
115    */
116   
117   Real gap_f = paper ()->get_var ("slur_x_gap");
118
119   Drul_array<Note_column*> extrema;
120   extrema[LEFT] = encompass_arr_[0];
121   extrema[RIGHT] = encompass_arr_.top ();
122
123   Direction d=LEFT;
124  
125 #define NORMAL_SLUR_b(dir) \
126   (extrema[dir]->stem_l_ \
127    && !extrema[dir]->stem_l_->transparent_b_  \
128    && extrema[dir]->head_l_arr_.size ()) 
129
130   do 
131     {
132       /*
133         broken slur
134        */
135       if (extrema[d] != spanned_drul_[d]) 
136         {
137           // ugh -- check if needed
138           dx_f_drul_[d] = -d 
139             *(spanned_drul_[d]->extent (X_AXIS).length () - 0.5 * notewidth_f);
140
141           // prebreak
142           if (d == RIGHT)
143             {
144               dx_f_drul_[LEFT] = spanned_drul_[LEFT]->extent (X_AXIS).length ();
145
146               // urg -- check if needed
147               if (encompass_arr_.size () > 1)
148                 dx_f_drul_[RIGHT] += notewidth_f;
149             }
150         }
151       /*
152         normal slur
153        */
154       else if (NORMAL_SLUR_b (d))
155         {
156           Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
157           dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_-> extent (Y_AXIS)[dir_]);
158           dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
159           if (dir_ == extrema[d]->stem_l_->dir_)
160             {
161               if (dir_ == d)
162                 dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
163               else
164                 dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
165             }
166         }
167         else 
168           {
169             Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
170             dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
171                                        [dir_]) * internote_f;
172             dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
173         }
174         dy_f_drul_[d] += dir_ * interline_f;
175         if (extrema[d]->stem_l_ && (dir_ == extrema[d]->stem_l_->dir_))
176           dy_f_drul_[d] -= dir_ * internote_f;
177       }
178   while (flip(&d) != LEFT);
179
180   // now that both are set, do dependent
181   do 
182     {
183       /*
184         broken slur
185        */
186       if (extrema[d] != spanned_drul_[d]) 
187         {
188           Direction u = d;
189           flip(&u);
190
191           // postbreak
192           if (d == LEFT)
193             dy_f_drul_[u] += dir_ * internote_f;
194
195           dy_f_drul_[d] = dy_f_drul_[u];
196         }
197      }
198   while (flip(&d) != LEFT);
199
200   /*
201     Slur should follow line of music
202    */
203   if (NORMAL_SLUR_b (LEFT) && NORMAL_SLUR_b (RIGHT)
204       && (extrema[LEFT]->stem_l_ != extrema[RIGHT]->stem_l_))
205     {
206       Real note_dy = extrema[RIGHT]->stem_l_->head_positions ()[dir_]
207         - extrema[LEFT]->stem_l_->head_positions ()[dir_];
208       Real dy = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
209       /*
210         Should we always follow note-heads, (like a tie)?
211         For now, only if the note_dy != slur_dy, we'll do
212         slur_dy := note_dy * factor.
213       */
214       if (sign (dy) != sign (note_dy))
215         {
216           Real damp_f = paper ()->get_var ("slur_slope_follow_music_factor");
217           Real realdy = note_dy * damp_f;
218           Direction adjust_dir = (Direction)(- dir_ * sign (realdy));
219           if (!adjust_dir)
220             adjust_dir = -dir_;
221           /*
222             adjust only if no beam gets in the way
223            */
224           if (!extrema[adjust_dir]->stem_l_->beam_l_
225               || (adjust_dir == extrema[adjust_dir]->stem_l_->dir_)
226               || (extrema[adjust_dir]->stem_l_->beams_i_drul_[-adjust_dir] < 1))
227             {
228               dy_f_drul_[adjust_dir] = dy_f_drul_[-adjust_dir]
229                 + 2 * adjust_dir * realdy;
230               Real dx = notewidth_f / 2;
231               if (adjust_dir != extrema[adjust_dir]->stem_l_->dir_)
232                 dx /= 2;
233               dx_f_drul_[adjust_dir] -= adjust_dir * dx;
234             }
235         }
236     }
237
238   /*
239     Avoid too steep slurs.
240    */
241   Real damp_f = paper ()->get_var ("slur_slope_damping");
242   Offset d_off = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
243     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
244   d_off[X_AXIS] += extent (X_AXIS).length ();
245
246   Real ratio_f = abs (d_off[Y_AXIS] / d_off[X_AXIS]);
247   if (ratio_f > damp_f)
248     dy_f_drul_[(Direction)(- dir_ * sign (d_off[Y_AXIS]))] +=
249       dir_ * (ratio_f - damp_f) * d_off[X_AXIS];
250 }
251
252 Array<Offset>
253 Slur::get_encompass_offset_arr () const
254 {
255   Real notewidth = paper ()->note_width () * 0.8;
256   Real gap = paper ()->get_var ("slur_x_gap");
257   Real internote = paper ()->internote_f ();
258
259   Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
260   left[X_AXIS] += encompass_arr_[0]->stem_l_->hpos_f ();
261
262   /*
263     <URG>
264     i don't understand these two, but *must* for symmetry 
265     look at encompass array: 
266        lilypond -D input/test/slur-symmetry*.ly
267        lilypond -D input/test/sleur.ly
268
269     do_post_processing should have calculated these into
270     dx_f_drul_[], no??
271
272    */
273
274   if (dir_ != encompass_arr_[0]->stem_l_->dir_)
275     left[X_AXIS] += - 0.5 * notewidth * encompass_arr_[0]->stem_l_->dir_
276       + gap;
277   else if (encompass_arr_[0]->stem_l_->dir_ == UP)
278     left[X_AXIS] -= notewidth;
279
280   if ((dir_ == encompass_arr_[0]->stem_l_->dir_) 
281     && (encompass_arr_[0]->stem_l_->dir_ == DOWN))
282     left[Y_AXIS] -= internote * encompass_arr_[0]->stem_l_->dir_;
283   /* </URG> */
284
285   Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
286     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
287   d[X_AXIS] += extent (X_AXIS).length ();
288
289   int first = 1;
290   int last = encompass_arr_.size () - 1;
291
292   // prebreak
293   if (encompass_arr_.top () != spanned_drul_[RIGHT])
294     last++;
295
296   // postbreak
297   if (encompass_arr_[0] != spanned_drul_[LEFT])
298     first--;
299
300   Array<Offset> notes;
301   notes.push (Offset (0,0));
302
303   for (int i = first; i < last; i++)
304     {
305       Encompass_info info (encompass_arr_[i], dir_);
306       notes.push (info.o_ - left);
307     }
308   Encompass_info info (encompass_arr_.top (), dir_);
309   // [encompass_arr_.size () - 1]
310   
311   // urg:
312   Slur* urg = (Slur*)this;
313   urg->interstaff_f_ = info.interstaff_f_;
314   
315   d[Y_AXIS] += interstaff_f_;
316
317   // prebreak
318   if (interstaff_f_ && (encompass_arr_.top () != spanned_drul_[RIGHT]))
319     {
320       Encompass_info info (encompass_arr_[encompass_arr_.size () - 1], dir_);
321       d[Y_AXIS] -= info.o_[Y_AXIS] - interstaff_f_;
322     }
323
324   notes.push (d);
325
326   return notes;
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 ()->get_var ("slur_x_minimum");
337
338   a.push (r);
339   return a;
340 }
341