]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 0.1.62
[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@stack.nl>
7     Jan Nieuwenhuizen <jan@digicash.com>
8 */
9
10 /*
11   [TODO]
12     * begin and end should be treated as a Script.
13     * damping
14     * slur from notehead to stemend: c''()b''
15  */
16
17 #include "slur.hh"
18 #include "scalar.hh"
19 #include "lookup.hh"
20 #include "paper-def.hh"
21 #include "note-column.hh"
22 #include "stem.hh"
23 #include "p-col.hh"
24 #include "molecule.hh"
25 #include "debug.hh"
26 #include "boxes.hh"
27 #include "bezier.hh"
28 #include "encompass-info.hh"
29 #include "main.hh"
30
31 IMPLEMENT_IS_TYPE_B1(Slur,Bow);
32
33 Slur::Slur ()
34 {
35 }
36
37 void
38 Slur::add (Note_column*n)
39 {
40   encompass_arr_.push (n);
41   add_dependency (n);
42 }
43
44 void
45 Slur::set_default_dir ()
46 {
47   dir_ = DOWN;
48   for (int i=0; i < encompass_arr_.size (); i ++) 
49     {
50       if (encompass_arr_[i]->dir_ < 0) 
51         {
52           dir_ = UP;
53           break;
54         }
55     }
56 }
57
58 void
59 Slur::do_add_processing ()
60 {
61   set_bounds (LEFT, encompass_arr_[0]);    
62   if (encompass_arr_.size () > 1)
63     set_bounds (RIGHT, encompass_arr_.top ());
64 }
65
66 void
67 Slur::do_pre_processing ()
68 {
69   // don't set directions
70 }
71
72 void
73 Slur::do_substitute_dependency (Score_elem*o, Score_elem*n)
74 {
75   int i;
76   while ((i = encompass_arr_.find_i ((Note_column*)o->item ())) >=0) 
77     {
78       if (n)
79         encompass_arr_[i] = (Note_column*)n->item ();
80       else
81         encompass_arr_.del (i);
82     }
83 }
84
85 static int 
86 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
87 {
88   return Item::left_right_compare (n1, n2);
89 }
90
91 void
92 Slur::do_post_processing ()
93 {
94   encompass_arr_.sort (Note_column_compare);
95   if (!dir_)
96     set_default_dir ();
97
98   Real interline_f = paper ()->interline_f ();
99   Real internote_f = interline_f / 2;
100   Real notewidth_f = paper ()->note_width ();
101   Real slur_min = paper ()->get_var ("slur_x_minimum");
102
103   /* 
104    [OSU]: slur and tie placement
105
106    slurs:
107    * x = centre of head (upside-down: inner raakpunt stem) - d * gap
108
109    * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
110      y = length >= 5ss : y next interline - d * 0.25 ss
111      --> height <= 5 length ?? we use <= 3 length, now...
112    */
113   
114   Real gap_f = paper ()->get_var ("slur_x_gap");
115
116   Drul_array<Note_column*> extrema;
117   extrema[LEFT] = encompass_arr_[0];
118   extrema[RIGHT] = encompass_arr_.top ();
119
120   Direction d=LEFT;
121  
122   do 
123     {
124       /*
125         broken slur
126        */
127       if (extrema[d] != spanned_drul_[d]) 
128         {
129           dx_f_drul_[d] = -d 
130             *(spanned_drul_[d]->width ().length () -0.5 * notewidth_f);
131
132           // prebreak
133           if (d == RIGHT)
134             {
135               dx_f_drul_[LEFT] = spanned_drul_[LEFT]->width ().length ();
136
137               // urg
138               if (encompass_arr_.size () > 1)
139                 dx_f_drul_[RIGHT] += notewidth_f;
140             }
141         }
142       /*
143         normal slur
144        */
145       else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_) 
146         {
147           dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_->height ()[dir_]);
148           dx_f_drul_[d] += 0.5 * notewidth_f - d * gap_f;
149           if (dir_ == extrema[d]->stem_l_->dir_)
150             {
151               if (dir_ == d)
152                 dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth_f;
153               else
154                 dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth_f;
155             }
156         }
157       else 
158         {
159           dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
160             [dir_]) * internote_f;
161         }
162       dy_f_drul_[d] += dir_ * interline_f;
163     }
164   while (flip(&d) != LEFT);
165
166   // now that both are set, do dependent
167   do 
168     {
169       /*
170         broken slur
171        */
172       if (extrema[d] != spanned_drul_[d]) 
173         {
174           Direction u = d;
175           flip(&u);
176
177           // postbreak
178           if (d == LEFT)
179             dy_f_drul_[u] += dir_ * internote_f;
180
181           dy_f_drul_[d] = dy_f_drul_[(Direction)-d];
182
183           // pre and post
184           if (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT] < slur_min)
185             {
186               dx_f_drul_[d] -= d * slur_min 
187                 - (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
188               dx_f_drul_[d] = dx_f_drul_[(Direction)-d] + d * slur_min;
189             }
190         }
191      }
192   while (flip(&d) != LEFT);
193 }
194
195 Array<Offset>
196 Slur::get_encompass_offset_arr () const
197 {
198   Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
199   left.x () += encompass_arr_[0]->stem_l_->hpos_f ();
200
201   Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
202     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
203   d.x () += width ().length ();
204
205   int first = 1;
206   int last = encompass_arr_.size () - 1;
207
208   // prebreak
209   if (encompass_arr_[0] != spanned_drul_[LEFT])
210     first--;
211
212   // postbreak
213   if (encompass_arr_.top () != spanned_drul_[RIGHT])
214     last++;
215
216 #define RESIZE_ICE
217 #ifndef RESIZE_ICE
218
219   Array<Offset> notes;
220   notes.push (Offset (0,0));
221
222   for (int i = first; i < last; i++)
223     {
224       Encompass_info info (encompass_arr_[i], dir_);
225       notes.push (info.o_ - left);
226     }
227   notes.push (d);
228
229 #else
230
231   int n = last - first + 2;
232   Array<Offset> notes (n);
233   notes[0] = Offset (0,0);
234
235   for (int i = first; i < last; i++)
236     {
237       Encompass_info info (encompass_arr_[i], dir_);
238       notes[i - first + 1] = info.o_ - left;
239     }
240   notes[n - 1] = Offset (d);
241
242 #endif
243
244   return notes;
245 }
246