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