]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.1.18
[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--1998, 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
34 Slur::Slur ()
35 {
36 }
37
38 void
39 Slur::add_column (Note_column*n)
40 {
41   if (!n->head_l_arr_.size ())
42     warning (_ ("Putting slur over rest."));
43   encompass_arr_.push (n);
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   Real slur_min = paper ()->get_var ("slur_x_minimum");
106
107   /* 
108    [OSU]: slur and tie placement
109
110    slurs:
111    * x = centre of head (upside-down: inner raakpunt stem) - d * gap
112
113    * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
114      y = length >= 5ss : y next interline - d * 0.25 ss
115      --> height <= 5 length ?? we use <= 3 length, now...
116    */
117   
118   Real gap_f = paper ()->get_var ("slur_x_gap");
119
120   Drul_array<Note_column*> extrema;
121   extrema[LEFT] = encompass_arr_[0];
122   extrema[RIGHT] = encompass_arr_.top ();
123
124   Direction d=LEFT;
125  
126   do 
127     {
128       /*
129         broken slur
130        */
131       if (extrema[d] != spanned_drul_[d]) 
132         {
133           // ugh -- check if needed
134           dx_f_drul_[d] = -d 
135             *(spanned_drul_[d]->extent (X_AXIS).length () - 0.5 * notewidth_f);
136
137           // prebreak
138           if (d == RIGHT)
139             {
140               dx_f_drul_[LEFT] = spanned_drul_[LEFT]->extent (X_AXIS).length ();
141
142               // urg -- check if needed
143               if (encompass_arr_.size () > 1)
144                 dx_f_drul_[RIGHT] += notewidth_f;
145             }
146         }
147       /*
148         normal slur
149        */
150       else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_ 
151                && extrema[d]->head_l_arr_.size ()) 
152         {
153           Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
154           dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_-> extent (Y_AXIS)[dir_]);
155           dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
156           if (dir_ == extrema[d]->stem_l_->dir_)
157             {
158               if (dir_ == d)
159                 dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
160               else
161                 dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
162             }
163         }
164         else 
165           {
166             Real notewidth_f = extrema[d]->extent (X_AXIS).length ();
167             dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
168                                        [dir_]) * internote_f;
169             dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
170         }
171         dy_f_drul_[d] += dir_ * interline_f;
172         if (extrema[d]->stem_l_ && (dir_ == extrema[d]->stem_l_->dir_))
173           dy_f_drul_[d] -= dir_ * internote_f;
174       }
175   while (flip(&d) != LEFT);
176
177   // now that both are set, do dependent
178   do 
179     {
180       /*
181         broken slur
182        */
183       if (extrema[d] != spanned_drul_[d]) 
184         {
185           Direction u = d;
186           flip(&u);
187
188           // postbreak
189           if (d == LEFT)
190             dy_f_drul_[u] += dir_ * internote_f;
191
192           dy_f_drul_[d] = dy_f_drul_[(Direction)-d];
193
194           // pre and post
195           if (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT] < slur_min)
196             {
197               dx_f_drul_[d] -= d * slur_min 
198                 - (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
199               dx_f_drul_[d] = dx_f_drul_[(Direction)-d] + d * slur_min;
200             }
201         }
202      }
203   while (flip(&d) != LEFT);
204
205   /*
206     Avoid too steep slurs.
207       * slur from notehead to stemend: c''()b''
208    */
209   Real damp_f = paper ()->get_var ("slur_slope_damping");
210   Offset d_off = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
211     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
212   d_off.x () += extent (X_AXIS).length ();
213
214   Real ratio_f = abs (d_off.y () / d_off.x ());
215   if (ratio_f > damp_f)
216     dy_f_drul_[(Direction)(- dir_ * sign (d_off.y ()))] -=
217       dir_ * (damp_f - ratio_f) * d_off.x ();
218 }
219
220 Array<Offset>
221 Slur::get_encompass_offset_arr () const
222 {
223   Real notewidth = paper ()->note_width () * 0.8;
224   Real gap = paper ()->get_var ("slur_x_gap");
225   Real internote = paper ()->internote_f ();
226
227   Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
228   left.x () += encompass_arr_[0]->stem_l_->hpos_f ();
229
230   /*
231     <URG>
232     i don't understand these two, but *must* for symmetry 
233     look at encompass array: 
234        lilypond -D input/test/slur-symmetry*.ly
235        lilypond -D input/test/sleur.ly
236
237     do_post_processing should have calculated these into
238     dx_f_drul_[], no??
239
240    */
241
242   if (dir_ != encompass_arr_[0]->stem_l_->dir_)
243     left.x () += - 0.5 * notewidth * encompass_arr_[0]->stem_l_->dir_
244       + gap;
245   else if (encompass_arr_[0]->stem_l_->dir_ == UP)
246     left.x () -= notewidth;
247
248   if ((dir_ == encompass_arr_[0]->stem_l_->dir_) 
249     && (encompass_arr_[0]->stem_l_->dir_ == DOWN))
250     left.y () -= internote * encompass_arr_[0]->stem_l_->dir_;
251   /* </URG> */
252
253   Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
254     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
255   d.x () += extent (X_AXIS).length ();
256
257   int first = 1;
258   int last = encompass_arr_.size () - 1;
259
260   // prebreak
261   if (encompass_arr_[0] != spanned_drul_[LEFT])
262     first--;
263
264   // postbreak
265   if (encompass_arr_.top () != spanned_drul_[RIGHT])
266     last++;
267
268   Array<Offset> notes;
269   notes.push (Offset (0,0));
270
271   for (int i = first; i < last; i++)
272     {
273       Encompass_info info (encompass_arr_[i], dir_);
274       notes.push (info.o_ - left);
275     }
276   notes.push (d);
277
278   return notes;
279 }
280