]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.3.24
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 /*
11   [TODO]
12     * begin and end should be treated as a/acknowledge Scripts.
13     * broken slur should have uniform trend
14  */
15
16 #include "directional-element-interface.hh"
17 #include "group-interface.hh"
18 #include "slur.hh"
19 #include "lookup.hh"
20 #include "paper-def.hh"
21 #include "note-column.hh"
22 #include "stem.hh"
23 #include "paper-column.hh"
24 #include "molecule.hh"
25 #include "debug.hh"
26 #include "box.hh"
27 #include "bezier.hh"
28 #include "bezier-bow.hh"
29 #include "main.hh"
30 #include "cross-staff.hh"
31 #include "group-interface.hh"
32
33 Slur::Slur ()
34 {
35   dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = 0.0;
36   dx_f_drul_[LEFT] = dx_f_drul_[RIGHT] = 0.0;
37   set_elt_property ("note-columns", SCM_EOL);
38 }
39
40 void
41 Slur::add_column (Note_column*n)
42 {
43   if (!gh_pair_p (n->get_elt_property ("note-heads")))
44     warning (_ ("Putting slur over rest.  Ignoring."));
45   else
46     {
47       Group_interface gi (this, "note-columns");
48       gi.add_element (n);
49       add_dependency (n);
50     }
51 }
52
53 Direction
54 Slur::get_default_dir () const
55 {
56   Link_array<Note_column> encompass_arr =
57     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
58   
59   Direction d = DOWN;
60   for (int i=0; i < encompass_arr.size (); i ++) 
61     {
62       if (encompass_arr[i]->dir () < 0) 
63         {
64           d = UP;
65           break;
66         }
67     }
68   return d;
69 }
70
71 void
72 Slur::do_add_processing ()
73 {
74   Link_array<Note_column> encompass_arr =
75     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
76   set_bounds (LEFT, encompass_arr[0]);    
77   if (encompass_arr.size () > 1)
78     set_bounds (RIGHT, encompass_arr.top ());
79 }
80
81
82
83 Offset
84 Slur::encompass_offset (Note_column const* col) const
85 {
86   Offset o;
87   Stem* stem_l = col->stem_l ();
88   Direction dir = directional_element (this).get ();
89   
90   if (!stem_l)
91     {
92       warning (_ ("Slur over rest?"));
93       o[X_AXIS] = col->hpos_f ();
94       o[Y_AXIS] = col->extent (Y_AXIS)[dir];
95       return o;  
96     }
97   Direction stem_dir = directional_element (stem_l).get ();
98   o[X_AXIS] = stem_l->hpos_f ();
99
100   /*
101     Simply set x to middle of notehead
102    */
103
104   o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
105
106   if ((stem_dir == dir)
107       && !stem_l->extent (Y_AXIS).empty_b ())
108     {
109       o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
110     }
111   else
112     {
113       o[Y_AXIS] = col->extent (Y_AXIS)[dir];
114     }
115
116   /*
117    leave a gap: slur mustn't touch head/stem
118    */
119   o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
120   o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
121   return o;
122 }
123
124 /*
125   ARGRARGRARGRARGAR!
126
127   Fixme
128  */
129 void
130 Slur::do_post_processing ()
131 {
132   Link_array<Note_column> encompass_arr =
133     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
134
135   if (!encompass_arr.size ())
136     {
137       set_elt_property ("transparent", SCM_BOOL_T);
138       set_empty (X_AXIS);
139       set_empty (Y_AXIS);
140       return;
141     }
142
143   if (!directional_element (this).get ())
144     directional_element (this).set (get_default_dir ());
145
146
147   /* 
148    Slur and tie placement [OSU]
149
150    Slurs:
151    * x = centre of head - d * x_gap_f
152
153    TODO:
154    * y = length < 5ss : horizontal tangent + d * 0.25 ss
155      y = length >= 5ss : y next interline - d * 0.25 ss
156    */
157
158   Real staff_space = paper_l ()->get_var ("interline");
159   Real half_staff_space = staff_space / 2;
160
161   Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
162   Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
163
164   Drul_array<Note_column*> note_column_drul;
165   note_column_drul[LEFT] = encompass_arr[0];
166   note_column_drul[RIGHT] = encompass_arr.top ();
167
168   bool fix_broken_b = false;
169
170   Direction my_dir = directional_element (this).get ();
171   
172   Direction d = LEFT;
173   do 
174     {
175       dx_f_drul_[d] = 0;
176       dy_f_drul_[d] = 0;
177       
178       if ((note_column_drul[d] == spanned_drul_[d])
179           && note_column_drul[d]->first_head ()
180           && (note_column_drul[d]->stem_l ()))
181         {
182           Stem* stem_l = note_column_drul[d]->stem_l ();
183           /*
184             side directly attached to note head;
185             no beam getting in the way
186           */
187           if ((stem_l->extent (Y_AXIS).empty_b ()
188                || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
189               && !((my_dir == stem_l->get_direction ())
190                    && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
191             {
192               dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
193               dx_f_drul_[d] -= d * x_gap_f;
194
195               if (stem_l->get_direction () != my_dir)
196                 {
197                   dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
198                 }
199               else
200                 {
201                   dy_f_drul_[d] = stem_l->chord_start_f ()
202                     + my_dir * half_staff_space;
203                 }
204               dy_f_drul_[d] += my_dir * y_gap_f;
205             }
206           /*
207             side attached to (visible) stem
208           */
209           else
210             {
211               dx_f_drul_[d] = stem_l->hpos_f ()
212                 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
213               /*
214                 side attached to beamed stem
215                */
216               if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
217                 {
218                   dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
219                   dy_f_drul_[d] += my_dir * 2 * y_gap_f;
220                 }
221               /*
222                 side attached to notehead, with stem getting in the way
223                */
224               else
225                 {
226                   dx_f_drul_[d] -= d * x_gap_f;
227                   
228                   dy_f_drul_[d] = stem_l->chord_start_f ()
229                     + my_dir * half_staff_space;
230                   dy_f_drul_[d] += my_dir * y_gap_f;
231                 }
232             }
233         }
234       /*
235         loose end
236       */
237       else
238         {
239           dx_f_drul_[d] = get_broken_left_end_align ();
240                 
241           /*
242             broken: should get y from other piece, so that slur
243             continues up/down trend
244
245             for now: be horizontal..
246           */
247           fix_broken_b = true;
248         }
249     }
250   while (flip (&d) != LEFT);
251
252   int cross_count =  cross_staff_count ();
253   bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
254
255   Drul_array<Offset> info_drul;
256   Drul_array<Real> interstaff_interval;
257
258   do
259     {
260       info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
261       interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
262                                                      this);
263     }
264   while (flip (&d) != LEFT);
265   
266   Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
267
268   if (fix_broken_b)
269     {
270       Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
271         RIGHT : LEFT;
272       dy_f_drul_[d] = info_drul[d][Y_AXIS];
273       if (!interstaff_b)
274         {
275           dy_f_drul_[d] -= interstaff_interval[d];
276           if (cross_count)      // interstaff_i  ? 
277             {
278               dy_f_drul_[LEFT] += interstaff_interval[d];
279               dy_f_drul_[RIGHT] += interstaff_interval[d];
280             }
281         }
282     }
283         
284   if (!fix_broken_b)
285     dy_f_drul_[RIGHT] += interstaff_f;
286
287
288
289   return; 
290
291   /*
292     Now we've got a fine slur
293     Catch and correct some ugly cases
294    */
295   String infix = interstaff_b ? "interstaff_" : "";
296   Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
297   Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
298   Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
299   Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
300
301   Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
302   Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
303   if (!fix_broken_b)
304     dy_f -= interstaff_f;
305
306   /*
307     Avoid too steep slurs.
308    */
309   Real slope_ratio_f = abs (dy_f / dx_f);
310   if (slope_ratio_f > slope_damp_f)
311     {
312       Direction d = (Direction)(- my_dir * (sign (dy_f)));
313       if (!d)
314         d = LEFT;
315       Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
316       /*
317         must never change sign of dy
318        */
319       damp_f = damp_f <? abs (dy_f);
320       dy_f_drul_[d] += my_dir * damp_f;
321     }
322
323   /*
324    Avoid too high slurs 
325
326    Wierd slurs may look a lot better after they have been
327    adjusted a bit.
328    So, we'll do this in 3 steps
329    */
330   for (int i = 0; i < 3; i++)
331     {
332       Bezier c (get_curve ());
333       
334       Offset size (c.extent (X_AXIS).length (),
335                    c.extent (Y_AXIS).length ());
336
337       dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
338       if (!fix_broken_b)
339         dy_f -= interstaff_f;
340
341       Real height_ratio_f = abs (size[Y_AXIS] / size[X_AXIS]);
342       if (height_ratio_f > height_damp_f)
343         {
344           Direction d = (Direction)(- my_dir * (sign (dy_f)));
345           if (!d)
346             d = LEFT;
347           /* take third step */
348           Real damp_f = (height_ratio_f - height_damp_f) * size[X_AXIS] / 3;
349           /*
350             if y positions at about the same height, correct both ends
351           */
352           if (abs (dy_f / dx_f ) < slope_damp_f)
353             {
354               dy_f_drul_[-d] += my_dir * damp_f;
355               dy_f_drul_[d] += my_dir * damp_f;
356             }
357           /*
358             don't change slope too much, would have been catched by slope damping
359           */
360           else
361             {
362               damp_f = damp_f <? abs (dy_f/2);
363               dy_f_drul_[d] += my_dir * damp_f;
364             }
365         }
366     }
367
368   /*
369     If, after correcting, we're close to stem-end...
370   */
371   Drul_array<Real> snapy_f_drul;
372   snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
373   Drul_array<Real> snapx_f_drul;
374   snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
375   Drul_array<bool> snapped_b_drul;
376   snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
377   do
378     {
379       Note_column * nc = note_column_drul[d];
380       if (nc == spanned_drul_[d]
381           && nc->stem_l ()
382           && nc->stem_l ()->get_direction () == my_dir
383           && abs (nc->stem_l ()->extent (Y_AXIS)[my_dir]
384                   - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
385               <= snap_f)
386         {
387           /*
388             prepare to attach to stem-end
389           */
390           snapx_f_drul[d] = nc->stem_l ()->hpos_f ()
391             - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
392
393           snapy_f_drul[d] = nc->stem_l ()->extent (Y_AXIS)[my_dir]
394             + interstaff_interval[d]
395             + my_dir * 2 * y_gap_f;
396           
397           snapped_b_drul[d] = true;
398         }
399     }
400   while (flip (&d) != LEFT);
401
402   /*
403     only use snapped positions if sign (dy) will not change
404     and dy doesn't change too much
405     */
406   if (!fix_broken_b)
407     dy_f += interstaff_f;
408
409
410   /*
411     (sigh)
412
413     More refactoring could be done.
414    */
415   Real maxsnap = abs (dy_f * snap_max_dy_f);
416   if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
417       && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
418       && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
419                     < maxsnap)))
420     {
421       dy_f_drul_ = snapy_f_drul;
422       dx_f_drul_ = snapx_f_drul;
423     }
424   else
425     do
426       {
427         Direction od = (Direction)-d;
428         if (snapped_b_drul[d]
429             && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
430             && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od]  - d * dy_f)
431                           < maxsnap)))
432           {
433             dy_f_drul_[d] = snapy_f_drul[d];
434             dx_f_drul_[d] = snapx_f_drul[d];
435           }
436       }
437     while (flip (&d) != LEFT);
438 }
439
440
441 int
442 Slur::cross_staff_count ()const
443 {
444   Link_array<Note_column> encompass_arr =
445     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
446
447   int k=0;
448
449   for (int i = 0; i < encompass_arr.size (); i++)
450     {
451       if (calc_interstaff_dist (encompass_arr[i], this))
452         k++;
453     }
454   return k;
455 }
456
457
458 Array<Offset>
459 Slur::get_encompass_offset_arr () const
460 {
461   Link_array<Note_column> encompass_arr =
462     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
463   
464   Array<Offset> offset_arr;
465 #if 0
466   /*
467     check non-disturbed slur
468     FIXME: x of ends off by a tiny bit!!
469   */
470   offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
471   offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
472   return offset_arr;
473 #endif
474   
475   Offset origin (relative_coordinate (0, X_AXIS), 0);
476
477   int first = 1;
478   int last = encompass_arr.size () - 2;
479
480   offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
481
482   /*
483     left is broken edge
484   */
485
486   int cross_count  = cross_staff_count ();
487   bool cross_b = cross_count && cross_count < encompass_arr.size ();
488   if (encompass_arr[0] != spanned_drul_[LEFT])
489     {
490       first--;
491       Real is   = calc_interstaff_dist (encompass_arr[0], this);
492       if (cross_b)
493         offset_arr[0][Y_AXIS] += is;
494     }
495
496   /*
497     right is broken edge
498   */
499   if (encompass_arr.top () != spanned_drul_[RIGHT])
500     {
501       last++;
502     }
503
504   for (int i = first; i <= last; i++)
505     {
506       Offset o (encompass_offset (encompass_arr[i]));
507       offset_arr.push (o - origin);
508     }
509
510   offset_arr.push (Offset (spanner_length ()+  dx_f_drul_[RIGHT],
511                            dy_f_drul_[RIGHT]));
512
513   return offset_arr;
514 }
515
516
517 Array<Rod>
518 Slur::get_rods () const
519 {
520   Array<Rod> a;
521   Rod r;
522   r.item_l_drul_ = spanned_drul_;
523   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
524
525   a.push (r);
526   return a;
527 }
528
529
530
531 #if 0
532 SCM
533 ugly_scm (Bezier  b) 
534 {
535   b.translate (-b.control_[0]);
536   Real alpha = b.control_[3].arg ();
537
538   b.rotate ( -alpha);
539   if (b.control_[1][Y_AXIS] < 0)
540     {
541       b.control_[1][Y_AXIS] *= -1;
542       b.control_[2][Y_AXIS] *= -1;      
543     }
544
545   Real len = b.control_[3][X_AXIS];
546   Real indent = 10 *b.control_[1][X_AXIS] / len ;
547   Real ht = 10 *b.control_[1][Y_AXIS] / len ;
548   
549   SCM res = scm_eval (scm_listify (ly_symbol2scm ("slur-ugly"), gh_double2scm (indent), gh_double2scm (ht), SCM_UNDEFINED ));
550
551   return res;
552 }
553 #endif
554
555
556 /*
557   Ugh should have dash-length + dash-period
558  */
559 Molecule*
560 Slur::do_brew_molecule_p () const
561 {
562   Real thick = paper_l ()->get_var ("slur_thickness");
563   Bezier one = get_curve ();
564
565   Molecule a;
566   SCM d =  get_elt_property ("dashed");
567   if (gh_number_p (d))
568     a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
569   else
570     a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
571
572 #if 0 
573   SCM u = ugly_scm (one);
574   if (gh_pair_p (u))
575     {
576       Molecule mark = lookup_l ()-> text ( "roman",
577                            to_str (gh_scm2double (gh_car (u)), "%0.2f") + "," +
578                            to_str(gh_scm2double (gh_cdr (u)), "%0.2f"),
579                            paper_l ());
580
581       mark.translate_axis (20 , Y_AXIS);
582       a.add_molecule (mark);
583     }
584 #endif
585   return new Molecule (a); 
586 }
587
588
589
590 Bezier
591 Slur::get_curve () const
592 {
593   Array<Offset> enc (get_encompass_offset_arr ());
594   Direction dir =  directional_element (this).get ();
595   Bezier_bow b (enc,dir);
596
597   b.ratio_ = paper_l ()->get_var ("slur_ratio");
598   b.height_limit_ = paper_l ()->get_var ("slur_height_limit");
599   b.rc_factor_ = paper_l ()->get_var ("slur_rc_factor");
600
601   b.calculate ();
602
603   Bezier  curve =  b.get_curve ();
604
605   Real x1 = enc[0][X_AXIS];
606   Real x2 = enc.top ()[X_AXIS];
607   
608   Real off = 0.0;
609   for (int i=1; i < enc.size ()-1; i++)
610     {
611       Real x = enc[i][X_AXIS];
612       if (x > x1 && x <x2)
613         {
614           Real y = curve.get_other_coordinate (X_AXIS, x);
615           off = off >? dir *  (enc[i][Y_AXIS] - y);
616         }
617     }
618   curve.translate (Offset (0, dir * off));
619   return curve;
620 }
621