]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 0.1.61
[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 = paper ()->interline_f ();
99   Real internote = interline / 2;
100   Real notewidth = paper ()->note_width ();
101   Real const SLUR_MIN = 2.0 * interline;
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    * suggested gap = ss / 5;
114    */
115   // jcn: 1/5 seems so small?
116   Real gap_f = interline / 2; // 5;
117   
118   Drul_array<Note_column*> extrema;
119   extrema[LEFT] = encompass_arr_[0];
120   extrema[RIGHT] = encompass_arr_.top ();
121
122   Direction d=LEFT;
123  
124   do 
125     {
126       /*
127         broken slur
128        */
129       if (extrema[d] != spanned_drul_[d]) 
130         {
131           dx_f_drul_[d] = -d 
132             *(spanned_drul_[d]->width ().length () -0.5*notewidth);
133           Direction u = d;
134           flip(&u);
135           if ((extrema[u] == spanned_drul_[u]) && extrema[u]->stem_l_)
136             {
137               dy_f_drul_[d] = extrema[u]->stem_l_->height ()[dir_];
138               dy_f_drul_[u] = extrema[u]->stem_l_->height ()[dir_];
139             }
140
141           // prebreak
142           if (d == RIGHT)
143             {
144               dx_f_drul_[LEFT] = spanned_drul_[LEFT]->width ().length ();
145 //            dx_f_drul_[LEFT] -= 2 * notewidth;
146
147               // urg
148               if (encompass_arr_.size () > 1)
149                 dx_f_drul_[RIGHT] += notewidth;
150             }
151
152           // postbreak
153           if (d == LEFT)
154             dy_f_drul_[d] += 2.0 * dir_ * internote;
155         }
156       /*
157         normal slur
158        */
159       else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_) 
160         {
161           dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_->height ()[dir_]);
162           dx_f_drul_[d] += 0.5 * notewidth - d * gap_f;
163           if (dir_ == extrema[d]->stem_l_->dir_)
164             {
165               if (dir_ == d)
166                 dx_f_drul_[d] += 0.5 * (dir_ * d) * d * notewidth;
167               else
168                 dx_f_drul_[d] += 0.25 * (dir_ * d) * d * notewidth;
169             }
170         }
171       else 
172         {
173           dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()
174             [dir_])* internote;
175         }
176       dy_f_drul_[d] += dir_ * interline;
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           // pre and post
189           if (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT] < SLUR_MIN)
190             {
191               dx_f_drul_[d] -= d * SLUR_MIN 
192                 - (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
193               dx_f_drul_[d] = dx_f_drul_[(Direction)-d] + d * SLUR_MIN;
194             }
195           dy_f_drul_[d] = dy_f_drul_[(Direction)-d];
196         }
197      }
198   while (flip(&d) != LEFT);
199 }
200
201 Array<Offset>
202 Slur::get_encompass_offset_arr () const
203 {
204   Offset left = Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]);
205   left.x () += encompass_arr_[0]->stem_l_->hpos_f ();
206
207   Offset d = Offset (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT],
208     dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
209   d.x () += width ().length ();
210
211   int first = 1;
212   int last = encompass_arr_.size () - 1;
213
214   // prebreak
215   if (encompass_arr_[0] != spanned_drul_[LEFT])
216     first--;
217
218   // postbreak
219   if (encompass_arr_.top () != spanned_drul_[RIGHT])
220     last++;
221
222 #define RESIZE_ICE
223 #ifndef RESIZE_ICE
224
225   Array<Offset> notes;
226   notes.push (Offset (0,0));
227
228   for (int i = first; i < last; i++)
229     {
230       Encompass_info info (encompass_arr_[i], dir_);
231       notes.push (info.o_ - left);
232     }
233   notes.push (d);
234
235 #else
236
237   int n = last - first + 2;
238   Array<Offset> notes (n);
239   notes[0] = Offset (0,0);
240
241   for (int i = first; i < last; i++)
242     {
243       Encompass_info info (encompass_arr_[i], dir_);
244       notes[i - first + 1] = info.o_ - left;
245     }
246   notes[n - 1] = Offset (d);
247
248 #endif
249
250   return notes;
251 }
252