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