]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.3.56
[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--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-bow.hh"
28 #include "main.hh"
29 #include "cross-staff.hh"
30 #include "group-interface.hh"
31 #include "staff-symbol-referencer.hh"
32
33 class Slur_bezier_bow : public Bezier_bow
34 {
35 public:
36   Slur_bezier_bow (Array<Offset> encompass, Direction dir);
37   Array<Real> area_x_gradients_array (Real area);
38   void blow_fit ();
39   Real enclosed_area_f () const;
40   Real fit_factor () const;
41   void minimise_enclosed_area (Paper_def* paper_l, Real default_height);
42 };
43
44 Slur_bezier_bow::Slur_bezier_bow (Array<Offset> encompass, Direction dir)
45   : Bezier_bow (encompass, dir)
46 {
47 }
48
49 void
50 Slur_bezier_bow::blow_fit ()
51 {
52   Real len = curve_.control_[3][X_AXIS]; 
53   Real h = curve_.control_[1][Y_AXIS] * fit_factor () / len;
54   curve_.control_[1][Y_AXIS] = h * len;
55   curve_.control_[2][Y_AXIS] = h * len;  
56   curve_.assert_sanity ();
57 }
58
59
60 Real
61 Slur_bezier_bow::enclosed_area_f () const
62 {
63   Real a = 0;
64   for (int i=0; i < encompass_.size (); i++)
65     {
66       Interval x;
67       Interval y;
68       if (i == 0)
69         {
70           x = Interval (0, encompass_[1][X_AXIS] / 2);
71           y = Interval (0,
72                         curve_.get_other_coordinate (X_AXIS,
73                                                      encompass_[1][X_AXIS]
74                                                      / 2));
75         }
76       else if (i == encompass_.size () - 1)
77         {
78           x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS])/2, 
79                         encompass_[i][X_AXIS]);
80           y = Interval (0,
81                         (curve_.get_other_coordinate (X_AXIS,
82                                                       (x[MIN] + x[MAX]) / 2)));
83         }
84       else
85         {
86           x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS]) / 2, 
87                         (encompass_[i][X_AXIS] + encompass_[i+1][X_AXIS]) / 2);
88           y = Interval (encompass_[i][Y_AXIS],
89                         (curve_.get_other_coordinate (X_AXIS, x[MIN])
90                          + curve_.get_other_coordinate (X_AXIS,
91                                                         (x[MIN] + x[MAX]) / 2)
92                          + curve_.get_other_coordinate (X_AXIS, x[MAX])) / 3);
93         }
94       
95       Real da = x.length () * y.length ();
96       a += da;
97     }
98   return a;
99 }
100
101 Array<Real>
102 Slur_bezier_bow::area_x_gradients_array (Real area)
103 {
104   Real len = curve_.control_[3][X_AXIS]; 
105   Real grow = len / 10.0;
106   Array<Real> da (2);
107   for (int i=0; i < 2; i++)
108     {
109       Real r = curve_.control_[i+1][X_AXIS];
110       curve_.control_[i+1][X_AXIS] += grow;
111       da[i] = (enclosed_area_f () - area) / grow;
112       curve_.control_[i+1][X_AXIS] = r; 
113     }
114   return da;
115 }
116
117 void
118 Slur_bezier_bow::minimise_enclosed_area (Paper_def* paper_l,
119                                          Real default_height)
120 {
121   Real length = curve_.control_[3][X_AXIS]; 
122   Real sb = paper_l->get_var ("slur_beautiful");
123   Real beautiful = length * default_height * sb;
124
125   DEBUG_OUT << to_str ("Beautiful: %f\n", beautiful);
126   DEBUG_OUT << to_str ("Length: %f\n", length);
127   DEBUG_OUT << to_str ("D-height: %f\n", default_height);
128   DEBUG_OUT << to_str ("FitFac: %f\n", fit_factor ());
129
130   if (fit_factor () > 1.0)
131     blow_fit ();
132   
133   Real pct_c0 = paper_l->get_var ("bezier_pct_c0");
134   Real pct_c3 = paper_l->get_var ("bezier_pct_c3");
135   Real pct_in_max = paper_l->get_var ("bezier_pct_in_max");
136   Real pct_out_max = paper_l->get_var ("bezier_pct_out_max");
137   Real steps = paper_l->get_var ("bezier_area_steps");
138
139   for (int i=0; i < steps; i++)
140     {
141       Real area = enclosed_area_f ();
142       if (!i)
143         DEBUG_OUT << to_str ("Init area: %f\n", area);
144
145       if (area <= beautiful)
146         break;
147
148       Array<Real> da = area_x_gradients_array (area);
149
150       // urg
151       Real pct = pct_c0 + pct_c3 * length * length * length;
152       pct *= (steps - i) / steps;
153       if (da[0] > 0 || da[1] < 0)
154         pct = pct <? pct_out_max;
155       else
156         pct = pct <? pct_in_max;
157
158       Real u = (abs (curve_.control_[1][X_AXIS] / da[0])
159                 <? abs ((curve_.control_[3][X_AXIS]
160                          - curve_.control_[2][X_AXIS]) / da[1]));
161
162       DEBUG_OUT << to_str ("pct: %f\n", pct);
163       DEBUG_OUT << to_str ("u: %f\n", u);
164
165       DEBUG_OUT << to_str ("da: (%f, %f)\n", da[0], da[1]);
166       DEBUG_OUT << to_str ("da*u: (%f, %f)\n", da[0]*u*pct, da[1]*u*pct);
167       DEBUG_OUT << to_str ("cx: (%f, %f)\n", curve_.control_[1][X_AXIS],
168                            curve_.control_[2][X_AXIS]);
169
170       curve_.control_[1][X_AXIS] -= da[0] * u * pct;
171       curve_.control_[2][X_AXIS] -= da[1] * u * pct;
172     }
173
174   Real area = enclosed_area_f ();
175   DEBUG_OUT << to_str ("Exarea: %f\n", area);
176 }
177
178
179
180 /*
181   max ( encompass.y / curve.y )
182   
183  */
184 Real
185 Slur_bezier_bow::fit_factor () const
186 {
187   Real x1 = encompass_[0][X_AXIS];
188   Real x2 = encompass_.top ()[X_AXIS];
189
190   Real factor = 0.0;
191   for (int i=1; i < encompass_.size ()-1; i++)
192     {
193       if (encompass_[i][X_AXIS] > x1 && encompass_[i][X_AXIS] < x2)
194         {
195          Real y = curve_.get_other_coordinate (X_AXIS, encompass_[i][X_AXIS]);
196          if (y>0)
197            {
198              Real f = encompass_[i][Y_AXIS] / y;
199              factor = factor >? f;
200            }
201         }
202     }
203
204
205   return factor;
206 }
207
208
209
210
211
212 /*
213   Slur
214 */
215
216 Slur::Slur (SCM s)
217   : Spanner (s)
218 {
219   // URG
220   dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = 0.0;
221   dx_f_drul_[LEFT] = dx_f_drul_[RIGHT] = 0.0;
222
223   set_elt_pointer ("note-columns", SCM_EOL);
224   set_elt_property ("control-points", SCM_EOL);
225 }
226
227 void
228 Slur::add_column (Note_column*n)
229 {
230   if (!gh_pair_p (n->get_elt_pointer ("note-heads")))
231     warning (_ ("Putting slur over rest.  Ignoring."));
232   else
233     {
234       Pointer_group_interface (this, "note-columns").add_element (n);
235       add_dependency (n);
236     }
237 }
238
239 void
240 Slur::de_uglyfy (Slur_bezier_bow* bb, Real default_height)
241 {
242   Real length = bb->curve_.control_[3][X_AXIS] ; 
243   Real ff = bb->fit_factor ();
244   for (int i = 1; i < 3; i++)
245     {
246       Real ind = abs (bb->curve_.control_[(i-1)*3][X_AXIS]
247                       - bb->curve_.control_[i][X_AXIS]) / length;
248       Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
249
250       Real f = default_height / length;
251       Real c1 = paper_l ()->get_var ("bezier_control1");
252       Real c2 = paper_l ()->get_var ("bezier_control2");
253       Real c3 = paper_l ()->get_var ("bezier_control3");
254       if (h > c1 * f)
255         {
256           h = c1 * f; 
257         }
258       else if (h > c2 + c3 * ind)
259         {
260           h = c2 + c3 * ind; 
261         }
262       
263       bb->curve_.control_[i][Y_AXIS] = h * length;
264     } 
265
266   bb->curve_.assert_sanity ();
267 }
268
269 Direction
270 Slur::get_default_dir () const
271 {
272   Link_array<Note_column> encompass_arr =
273     Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
274   
275   Direction d = DOWN;
276   for (int i=0; i < encompass_arr.size (); i ++) 
277     {
278       if (encompass_arr[i]->dir () < 0) 
279         {
280           d = UP;
281           break;
282         }
283     }
284   return d;
285 }
286
287 void
288 Slur::do_add_processing ()
289 {
290   Link_array<Note_column> encompass_arr =
291     Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
292
293   if (encompass_arr.size ())
294     {
295       set_bound (LEFT, encompass_arr[0]);    
296       if (encompass_arr.size () > 1)
297         set_bound (RIGHT, encompass_arr.top ());
298     }
299 }
300
301
302
303 Offset
304 Slur::encompass_offset (Note_column const* col) const
305 {
306   Offset o;
307   Stem* stem_l = col->stem_l ();
308   Direction dir = directional_element (this).get ();
309   
310   if (!stem_l)
311     {
312       warning (_ ("Slur over rest?"));
313      o[X_AXIS] = col->relative_coordinate (0, X_AXIS);
314       o[Y_AXIS] = col->extent (Y_AXIS)[dir];
315       return o;  
316     }
317   Direction stem_dir = directional_element (stem_l).get ();
318   o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
319
320   /*
321     Simply set x to middle of notehead
322    */
323
324   o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
325
326   if ((stem_dir == dir)
327       && !stem_l->extent (Y_AXIS).empty_b ())
328     {
329       o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
330     }
331   else
332     {
333       o[Y_AXIS] = col->extent (Y_AXIS)[dir];
334     }
335
336   /*
337    leave a gap: slur mustn't touch head/stem
338    */
339   o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
340   o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
341   return o;
342 }
343
344 void
345 Slur::after_line_breaking ()
346 {
347   set_extremities ();
348   set_control_points ();
349
350
351 /*
352   urg
353   FIXME
354  */
355 void
356 Slur::set_extremities ()
357 {
358   Link_array<Note_column> encompass_arr =
359     Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
360
361   if (!encompass_arr.size ())
362     {
363       suicide();
364       return;
365     }
366
367   if (!directional_element (this).get ())
368     directional_element (this).set (get_default_dir ());
369
370
371   /* 
372    Slur and tie placement [OSU]
373
374    Slurs:
375    * x = centre of head - d * x_gap_f
376
377    TODO:
378    * y = length < 5ss : horizontal tangent + d * 0.25 ss
379      y = length >= 5ss : y next interline - d * 0.25 ss
380    */
381
382   Real staff_space = paper_l ()->get_var ("interline");
383   Real half_staff_space = staff_space / 2;
384
385   Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
386   Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
387
388   Drul_array<Note_column*> note_column_drul;
389   note_column_drul[LEFT] = encompass_arr[0];
390   note_column_drul[RIGHT] = encompass_arr.top ();
391
392   bool fix_broken_b = false;
393
394   Direction my_dir = directional_element (this).get ();
395   
396   Direction d = LEFT;
397   do 
398     {
399       dx_f_drul_[d] = 0;
400       dy_f_drul_[d] = 0;
401       
402       if ((note_column_drul[d] == get_bound (d))
403           && note_column_drul[d]->first_head ()
404           && (note_column_drul[d]->stem_l ()))
405         {
406           Stem* stem_l = note_column_drul[d]->stem_l ();
407           /*
408             side directly attached to note head;
409             no beam getting in the way
410           */
411           if ((stem_l->extent (Y_AXIS).empty_b ()
412                || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
413               && !((my_dir == stem_l->get_direction ())
414                    && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
415             {
416               dx_f_drul_[d] = get_bound (d)->extent (X_AXIS).length () / 2;
417               dx_f_drul_[d] -= d * x_gap_f;
418
419               if (stem_l->get_direction () != my_dir)
420                 {
421                   dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
422                 }
423               else
424                 {
425                   dy_f_drul_[d] = stem_l->chord_start_f ()
426                     + my_dir * half_staff_space;
427                 }
428               dy_f_drul_[d] += my_dir * y_gap_f;
429             }
430           /*
431             side attached to (visible) stem
432           */
433           else
434             {
435               dx_f_drul_[d] = stem_l->relative_coordinate (0, X_AXIS)
436                 - get_bound (d)->relative_coordinate (0, X_AXIS);
437               /*
438                 side attached to beamed stem
439                */
440               if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
441                 {
442                   dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
443                   dy_f_drul_[d] += my_dir * 2 * y_gap_f;
444                 }
445               /*
446                 side attached to notehead, with stem getting in the way
447                */
448               else
449                 {
450                   dx_f_drul_[d] -= d * x_gap_f;
451                   
452                   dy_f_drul_[d] = stem_l->chord_start_f ()
453                     + my_dir * half_staff_space;
454                   dy_f_drul_[d] += my_dir * y_gap_f;
455                 }
456             }
457         }
458       /*
459         loose end
460       */
461       else
462         {
463           dx_f_drul_[d] = get_broken_left_end_align ();
464                 
465           /*
466             broken: should get y from other piece, so that slur
467             continues up/down trend
468
469             for now: be horizontal..
470           */
471           fix_broken_b = true;
472         }
473     }
474   while (flip (&d) != LEFT);
475
476   int cross_count =  cross_staff_count ();
477   bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
478
479   Drul_array<Offset> info_drul;
480   Drul_array<Real> interstaff_interval;
481
482   do
483     {
484       info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
485       interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
486                                                      this);
487     }
488   while (flip (&d) != LEFT);
489   
490   Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
491
492   if (fix_broken_b)
493     {
494       Direction d = (encompass_arr.top () != get_bound (RIGHT)) ?
495         RIGHT : LEFT;
496       dy_f_drul_[d] = info_drul[d][Y_AXIS];
497       if (!interstaff_b)
498         {
499           dy_f_drul_[d] -= interstaff_interval[d];
500           if (cross_count)      // interstaff_i  ? 
501             {
502               dy_f_drul_[LEFT] += interstaff_interval[d];
503               dy_f_drul_[RIGHT] += interstaff_interval[d];
504             }
505         }
506     }
507         
508   if (!fix_broken_b)
509     dy_f_drul_[RIGHT] += interstaff_f;
510 }
511
512
513 int
514 Slur::cross_staff_count ()const
515 {
516   Link_array<Note_column> encompass_arr =
517     Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
518
519   int k=0;
520
521   for (int i = 0; i < encompass_arr.size (); i++)
522     {
523       if (calc_interstaff_dist (encompass_arr[i], this))
524         k++;
525     }
526   return k;
527 }
528
529
530 Array<Offset>
531 Slur::get_encompass_offset_arr () const
532 {
533   Link_array<Note_column> encompass_arr =
534     Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
535   
536   Array<Offset> offset_arr;
537
538 #if 0
539   /*
540     check non-disturbed slur
541     FIXME: x of ends off by a tiny bit!!
542   */
543   offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
544   offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
545   return offset_arr;
546 #endif
547   
548   Offset origin (relative_coordinate (0, X_AXIS), 0);
549
550   int first = 1;
551   int last = encompass_arr.size () - 2;
552
553   offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
554
555   /*
556     left is broken edge
557   */
558
559   int cross_count  = cross_staff_count ();
560   bool cross_b = cross_count && cross_count < encompass_arr.size ();
561   if (encompass_arr[0] != get_bound (LEFT))
562     {
563       first--;
564       Real is   = calc_interstaff_dist (encompass_arr[0], this);
565       if (cross_b)
566         offset_arr[0][Y_AXIS] += is;
567     }
568
569   /*
570     right is broken edge
571   */
572   if (encompass_arr.top () != get_bound (RIGHT))
573     {
574       last++;
575     }
576
577   for (int i = first; i <= last; i++)
578     {
579       Offset o (encompass_offset (encompass_arr[i]));
580       offset_arr.push (o - origin);
581     }
582
583   offset_arr.push (Offset (spanner_length ()+  dx_f_drul_[RIGHT],
584                            dy_f_drul_[RIGHT]));
585
586   return offset_arr;
587 }
588
589
590 Array<Rod>
591 Slur::get_rods () const
592 {
593   Array<Rod> a;
594   Rod r;
595   
596   r.item_l_drul_[LEFT] = get_bound (LEFT);
597   r.item_l_drul_[RIGHT] = get_bound (RIGHT);
598   r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
599
600   a.push (r);
601   return a;
602 }
603
604
605
606 MAKE_SCHEME_SCORE_ELEMENT_CALLBACKS(Slur);
607
608 /*
609   Ugh should have dash-length + dash-period
610  */
611 Molecule
612 Slur::do_brew_molecule () const
613 {
614   Real thick = paper_l ()->get_var ("slur_thickness");
615   Bezier one = get_curve ();
616
617   Molecule a;
618   SCM d =  get_elt_property ("dashed");
619   if (gh_number_p (d))
620     a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
621   else
622     a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
623
624   return a;
625 }
626
627 void
628 Slur::set_control_points ()
629 {
630   Slur_bezier_bow bb (get_encompass_offset_arr (),
631                       directional_element (this).get ());
632
633   Real staff_space = Staff_symbol_referencer_interface (this).staff_space ();
634   Real h_inf = paper_l ()->get_var ("slur_height_limit_factor") * staff_space;
635   Real r_0 = paper_l ()->get_var ("slur_ratio");
636
637   bb.set_default_bezier (h_inf, r_0);
638
639   if (bb.fit_factor () > 1.0)
640     {
641       Real length = bb.curve_.control_[3][X_AXIS]; 
642       Real default_height = bb.get_default_height (h_inf, r_0, length);
643       bb.minimise_enclosed_area (paper_l(), default_height);
644       
645       Real bff = paper_l ()->get_var ("slur_force_blowfit");
646       bb.curve_.control_[1][Y_AXIS] *= bff;
647       bb.curve_.control_[2][Y_AXIS] *= bff;
648       bb.blow_fit ();
649
650       Real sb = paper_l ()->get_var ("slur_beautiful");
651       Real beautiful = length * default_height * sb;
652       Real area = bb.enclosed_area_f ();
653       
654       /*
655         Slurs that fit beautifully are not ugly
656       */
657       if (area > beautiful)
658         de_uglyfy (&bb, default_height);
659     }
660
661   Bezier b = bb.get_bezier ();
662
663
664   SCM controls = SCM_EOL;
665   for (int i= 4; i--;)
666     controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
667
668   set_elt_property ("control-points", controls);
669 }
670   
671   
672 Bezier
673 Slur::get_curve () const
674 {
675   Bezier b;
676   int i = 0;
677
678   if (!directional_element (this).get ())
679     ((Slur*)this)->set_extremities ();
680   
681   if (!gh_pair_p (get_elt_property ("control-points")))
682     ((Slur*)this)->set_control_points ();
683   
684   
685   for (SCM s= get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
686     {
687       b.control_[i] = ly_scm2offset (gh_car (s));
688       i++;
689     }
690   
691   Array<Offset> enc (get_encompass_offset_arr ());
692   Direction dir = directional_element (this).get ();
693   
694   Real x1 = enc[0][X_AXIS];
695   Real x2 = enc.top ()[X_AXIS];
696   
697   Real off = 0.0;
698   for (int i=1; i < enc.size ()-1; i++)
699     {
700       Real x = enc[i][X_AXIS];
701       if (x > x1 && x <x2)
702         {
703           Real y = b.get_other_coordinate (X_AXIS, x);
704           off = off >? dir *  (enc[i][Y_AXIS] - y);
705         }
706     }
707   b.translate (Offset (0, dir * off));
708   return b;
709 }
710