]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 0.1.57
[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 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 /*
10   TODO:
11   
12   think about crossing stems.
13   Begin and end should be treated as a Script.
14  */
15 #include "slur.hh"
16 #include "scalar.hh"
17 #include "lookup.hh"
18 #include "paper-def.hh"
19 #include "note-column.hh"
20 #include "stem.hh"
21 #include "p-col.hh"
22 #include "molecule.hh"
23 #include "debug.hh"
24 #include "boxes.hh"
25 #include "bezier.hh"
26 #include "main.hh"
27
28 IMPLEMENT_IS_TYPE_B1(Slur,Spanner);
29
30 void
31 Slur::add (Note_column*n)
32 {
33   encompass_arr_.push (n);
34   add_dependency (n);
35 }
36
37 void
38 Slur::set_default_dir ()
39 {
40   dir_ = DOWN;
41   for (int i=0; i < encompass_arr_.size (); i ++) 
42     {
43       if (encompass_arr_[i]->dir_ < 0) 
44         {
45           dir_ = UP;
46           break;
47         }
48     }
49 }
50
51 void
52 Slur::do_add_processing ()
53 {
54   set_bounds (LEFT, encompass_arr_[0]);    
55   if (encompass_arr_.size () > 1)
56     set_bounds (RIGHT, encompass_arr_.top ());
57 }
58
59 void
60 Slur::do_pre_processing ()
61 {
62   // don't set directions
63 }
64
65 void
66 Slur::do_substitute_dependency (Score_elem*o, Score_elem*n)
67 {
68   int i;
69   while ((i = encompass_arr_.find_i ((Note_column*)o->item ())) >=0) 
70     {
71       if (n)
72         encompass_arr_[i] = (Note_column*)n->item ();
73       else
74         encompass_arr_.del (i);
75     }
76 }
77
78 static int 
79 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
80 {
81   return Item::left_right_compare (n1, n2);
82 }
83
84 void
85 Slur::do_post_processing ()
86 {
87   encompass_arr_.sort (Note_column_compare);
88   if (!dir_)
89     set_default_dir ();
90   Real interline_f = paper ()->interline_f ();
91   Real inter_f = interline_f / 2;
92
93   /* 
94    [OSU]: slur and tie placement
95
96    slurs:
97    * x = centre of head (upside-down: inner raakpunt stem) - d * gap
98
99    * y = length < 5ss : horizontal raakpunt + d * 0.25 ss
100      y = length >= 5ss : y next interline - d * 0.25 ss
101      --> height <= 5 length ?? we use <= 3 length, now...
102
103    * suggested gap = ss / 5;
104    */
105   // jcn: 1/5 seems so small?
106   Real gap_f = interline_f / 2; // 5;
107   
108   Drul_array<Note_column*> extrema;
109   extrema[LEFT] = encompass_arr_[0];
110   extrema[RIGHT] = encompass_arr_.top ();
111
112   Direction d=LEFT;
113   Real nw_f = paper ()->note_width ();
114  
115   do 
116     {
117       if (extrema[d] != spanned_drul_[d]) 
118         {
119           dx_f_drul_[d] = -d 
120             *(spanned_drul_[d]->width ().length () -0.5*nw_f);
121         }
122       else if (extrema[d]->stem_l_ && !extrema[d]->stem_l_->transparent_b_) 
123         {
124           dy_f_drul_[d] = (int)rint (extrema[d]->stem_l_->height ()[dir_]);
125           /* normal slur from notehead centre to notehead centre, minus gap */
126           // ugh: diff between old and new slurs
127           if (!experimental_features_global_b)
128             dx_f_drul_[d] += -d * gap_f;
129           else
130             dx_f_drul_[d] += 0.5 * nw_f - d * gap_f;
131         }
132       else 
133         {
134           dy_f_drul_[d] = (int)rint (extrema[d]->head_positions_interval ()[dir_])* inter_f;
135         }
136       dy_f_drul_[d] += dir_ * interline_f;
137     }
138   while (flip(&d) != LEFT);
139 }
140
141 Real
142 Slur::height_f () const
143 {
144   Bezier_bow bow (paper ());
145   Array<Offset> notes = get_notes ();
146   bow.set (notes, dir_);
147
148   Real height = 0;
149   Real dy1 = bow.calc_f (height);
150   if (!dy1)
151     return height;
152
153   height = dy1;
154   bow.set (notes, dir_);
155   Real dy2 = bow.calc_f (height);
156   if (!dy2)
157     return height;
158
159   if (abs (dy2 - dy1) < paper ()->rule_thickness ())
160     return height;
161   
162   /*
163     Assume 
164       dy = B (h) 
165     with 
166       B (h) = a * h + b;
167
168     Then we get for height = h{dy=0}
169    */
170   Real a = (dy2 - dy1) / dy1;
171   Real b = dy1;
172   height = -b / a;
173     
174   if (check_debug && !monitor->silent_b ("Slur")) 
175     { 
176       cout << "************" << endl;
177       cout << "dy1: " << dy1 << endl;
178       cout << "dy2: " << dy2 << endl;
179       cout << "a: " << a << endl;
180       cout << "b: " << b << endl;
181       cout << "h: " << height << endl;
182     }
183
184   return height;
185 }
186
187 Molecule*
188 Slur::brew_molecule_p () const
189 {
190   if (!experimental_features_global_b)
191     return Bow::brew_molecule_p ();
192
193   Molecule* mol_p = new Molecule;
194   
195   Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
196   
197   Real dx_f = width ().length ();
198   dx_f += (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
199   
200   Atom a = paper ()->lookup_l ()->control_slur (get_controls (), dx_f, dy_f);
201
202   Real interline_f = paper ()->interline_f ();
203   Real gap_f = interline_f / 2; // 5;
204   Real nw_f = paper ()->note_width ();
205   a.translate (Offset (dx_f + 0.5 * nw_f + gap_f, dy_f + dy_f_drul_[LEFT]));
206   mol_p->add (a);
207   return mol_p;
208 }
209
210 Array<Offset>
211 Slur::get_notes () const
212 {
213   Real interline = paper ()->interline_f ();
214   Real notewidth = paper ()->note_width ();
215   Real internote = interline / 2;
216
217   Stem* left_stem = encompass_arr_[0]->stem_l_;
218   Real left_x = left_stem->hpos_f ();
219   // ugh, do bow corrections (see brew_mol)
220   left_x += dx_f_drul_[LEFT] + 0.5 * notewidth;
221
222   // ugh, do bow corrections (see brew_mol)
223   Real left_y = dy_f_drul_[LEFT];
224   // ugh, where does this asymmetry come from?
225   if (dir_ == DOWN)
226     left_y -= dir_ * internote;
227
228   /*
229     urg, corrections for broken slurs: extra begin or end position 
230    */
231   int first = 0;
232   int n = encompass_arr_.size ();
233   if (encompass_arr_[0] != spanned_drul_[LEFT])
234     {
235       n += 1;
236       first = 1;
237       left_x = spanned_drul_[LEFT]->width ().length ();
238       left_y = 0;
239     }
240   if (encompass_arr_.top () != spanned_drul_[RIGHT])
241       n += 1;
242
243   Array<Offset> notes;
244   notes.set_size (n);
245
246   Real dx = width ().length ();
247   dx += (dx_f_drul_[RIGHT] - dx_f_drul_[LEFT]);
248   dx = dx <? 1000;
249   dx = dx >? 2 * interline;
250     
251   Real dy = (dy_f_drul_[RIGHT] - dy_f_drul_[LEFT]);
252   if (abs (dy) > 1000)
253     dy = sign (dy) * 1000;
254
255   notes[0].x () = 0;
256   notes[0].y () = 0;
257   notes[n - 1].x () = dx;
258   notes[n - 1].y () = dy;
259   for (int i = 1; i < n - 1; i++)
260     {
261       Stem* stem = encompass_arr_[i - first]->stem_l_;
262       /* 
263         set x to middle of notehead or on exact x position of stem,
264         according to slur direction
265            */
266       Real x = stem->hpos_f () - left_x + notewidth / 2;
267       if (stem->dir_ != dir_)
268         x += notewidth / 2;
269       else if (stem->dir_ == UP)
270         x += notewidth;
271       Real y = stem->dir_ == dir_ ? stem->stem_end_f ()
272         : stem->stem_begin_f () + 2.5 * dir_;
273
274       /*
275         leave a gap: slur mustn't touch head/stem
276        */
277       y += 2.5 * dir_;
278       y *= internote;
279       y -= left_y;
280
281       notes[i].x () = x;
282       notes[i].y () = y;
283     }
284   return notes;
285 }
286
287 Array<Offset>
288 Slur::get_controls () const
289 {
290   Bezier_bow b (paper ());
291   b.set (get_notes (), dir_);
292   b.calc ();
293   Array<Offset> controls;
294   controls.set_size (8);
295   for (int i = 0; i < 4; i++)
296     controls[i] = b.control_[i];
297   for (int i = 0; i < 4; i++)
298     controls[i + 4] = b.return_[i];
299   return controls;
300 }
301