]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
release: 1.3.81
[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     * fix broken interstaff slurs
13     * begin and end should be treated as a/acknowledge Scripts.
14     * broken slur should have uniform trend
15     * smart changing of endings
16     * smart changing of (Y-?)offsets to avoid ugly beziers
17        (along-side-stem)
18  */
19
20 #include "directional-element-interface.hh"
21 #include "group-interface.hh"
22 #include "slur.hh"
23 #include "lookup.hh"
24 #include "paper-def.hh"
25 #include "note-column.hh"
26 #include "stem.hh"
27 #include "paper-column.hh"
28 #include "molecule.hh"
29 #include "debug.hh"
30 #include "slur-bezier-bow.hh"
31 #include "main.hh"
32 #include "group-interface.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "spanner.hh"
35
36
37 void
38 Slur::set_interface (Score_element*me)
39 {
40   me->set_elt_property ("attachment", gh_cons (SCM_BOOL_F, SCM_BOOL_F));
41   me->set_interface (ly_symbol2scm ("slur-interface"));
42 }
43
44 void
45 Slur::add_column (Score_element*me, Score_element*n)
46 {
47   if (!gh_pair_p (n->get_elt_property ("note-heads")))
48     warning (_ ("Putting slur over rest.  Ignoring."));
49   else
50     {
51       Pointer_group_interface (me, "note-columns").add_element (n);
52       me->add_dependency (n);
53     }
54
55   add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*>(n));
56 }
57
58 void
59 Slur::de_uglyfy (Score_element*me, Slur_bezier_bow* bb, Real default_height)
60 {
61   Real length = bb->curve_.control_[3][X_AXIS] ; 
62   Real ff = bb->fit_factor ();
63   for (int i = 1; i < 3; i++)
64     {
65       Real ind = abs (bb->curve_.control_[(i-1)*3][X_AXIS]
66                       - bb->curve_.control_[i][X_AXIS]) / length;
67       Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
68
69       Real f = default_height / length;
70       Real c1 = me->paper_l ()->get_var ("bezier_control1");
71       Real c2 = me->paper_l ()->get_var ("bezier_control2");
72       Real c3 = me->paper_l ()->get_var ("bezier_control3");
73       if (h > c1 * f)
74         {
75           h = c1 * f; 
76         }
77       else if (h > c2 + c3 * ind)
78         {
79           h = c2 + c3 * ind; 
80         }
81       
82       bb->curve_.control_[i][Y_AXIS] = h * length;
83     } 
84
85   bb->curve_.assert_sanity ();
86 }
87
88 Direction
89 Slur::get_default_dir (Score_element*me) 
90 {
91   Link_array<Score_element> encompass_arr =
92     Pointer_group_interface__extract_elements (me, (Score_element*)0, "note-columns");
93   
94   Direction d = DOWN;
95   for (int i=0; i < encompass_arr.size (); i ++) 
96     {
97       if (Note_column::dir (encompass_arr[i]) < 0) 
98         {
99           d = UP;
100           break;
101         }
102     }
103   return d;
104 }
105
106
107 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking);
108 SCM
109 Slur::after_line_breaking (SCM smob)
110 {
111   Score_element *me = unsmob_element (smob);
112   if (!scm_ilength (me->get_elt_property ("note-columns")))
113     {
114       me->suicide ();
115       return SCM_UNSPECIFIED;
116     }
117   set_extremities (me);
118   set_control_points (me);
119   return SCM_UNSPECIFIED;
120
121
122 void
123 Slur::set_extremities (Score_element*me)
124 {
125   if (!Directional_element_interface (me).get ())
126     Directional_element_interface (me).set (get_default_dir (me));
127
128   Direction dir = LEFT;
129   do 
130     {
131       if (!gh_symbol_p (index_cell (me->get_elt_property ("attachment"), dir)))
132         {
133           
134           // for (SCM s = get_elt_property ("slur-extremity-rules"); s != SCM_EOL; s = gh_cdr (s))
135           for (SCM s = scm_eval2 (ly_symbol2scm ("slur-extremity-rules"),
136                                   SCM_EOL);
137                s != SCM_EOL; s = gh_cdr (s))
138             {
139               SCM r = gh_call2 (gh_caar (s), me->self_scm (),
140                                  gh_int2scm ((int)dir));
141               if (r != SCM_BOOL_F)
142                 {
143                   index_set_cell (me->get_elt_property ("attachment"), dir,
144                                   gh_cdar (s));
145                   break;
146                 }
147             }
148         }
149     }
150   while (flip (&dir) != LEFT);
151 }
152
153 Real
154 Slur::get_first_notecolumn_y (Score_element *me, Direction dir)
155 {
156   Score_element *col = dir == LEFT
157     ? unsmob_element (gh_car (scm_reverse (me->get_elt_property
158                                            ("note-columns"))))
159     : unsmob_element
160     (gh_car (me->get_elt_property ("note-columns")));
161   
162   Score_element *common[] =
163   {
164     0,
165     me->common_refpoint (col, Y_AXIS)
166   };
167   Real y;
168   if (col == ((Spanner*)me)->get_bound (dir))
169     {
170       y = get_attachment (me, dir, common)[Y_AXIS];
171     }
172   else
173     {
174       y = encompass_offset (me, col, common)[Y_AXIS]
175         - me->relative_coordinate (common[Y_AXIS], Y_AXIS); 
176     }
177   return y;
178 }
179
180 Offset
181 Slur::broken_trend_offset (Score_element *me, Direction dir)
182 {
183   /*
184     A broken slur should maintain the same vertical trend
185     the unbroken slur would have had.
186   */
187   Offset o;
188   if (Spanner *mother =  dynamic_cast<Spanner*> (me->original_l_))
189     {
190       for (int i = dir == LEFT ? 0 : mother->broken_into_l_arr_.size ();
191            dir == LEFT ? i < mother->broken_into_l_arr_.size () : i;
192            dir == LEFT ? i++ : --i)
193         {
194           if (mother->broken_into_l_arr_[i - dir] == me)
195             {
196               Score_element *neighbour = mother->broken_into_l_arr_[i];
197               if (dir == RIGHT)
198                 neighbour->set_elt_property ("direction",
199                                              me->get_elt_property ("direction"));
200               Real neighbour_y = get_first_notecolumn_y (neighbour, dir);
201               Real y = get_first_notecolumn_y (me, -dir);
202               o = Offset (0, (y + neighbour_y) / 2);
203               break;
204             }
205         }
206     }
207   return o;
208 }
209
210 Offset
211 Slur::get_attachment (Score_element*me,Direction dir,
212                       Score_element **common) 
213 {
214   SCM s = me->get_elt_property ("attachment");
215   if (!gh_symbol_p (index_cell (s, dir)))
216     {
217       set_extremities (me);
218       s = me->get_elt_property ("attachment");
219     }
220   SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
221   Spanner*sp = dynamic_cast<Spanner*>(me);
222   String str = ly_symbol2string (a);
223   Real ss = Staff_symbol_referencer::staff_space ((Score_element*)me);
224   Real hs = ss / 2.0;
225   Offset o;
226   
227   Score_element *stem = 0;
228   if (Note_column::has_interface (sp->get_bound (dir)))
229     {
230       Score_element * n =sp->get_bound (dir);
231       if (Score_element *stem = Note_column::stem_l (n))
232         {
233
234           if (str == "head")
235             {
236               o = Offset (0, Stem::head_positions (stem)
237                           [Directional_element_interface (me).get ()] * hs);
238               /*
239                 Default position is centered in X, on outer side of head Y
240                */
241               o += Offset (0.5 * n->extent (X_AXIS).length (),
242                            0.5 * ss * Directional_element_interface (me).get ());
243             }
244           else if (str == "alongside-stem")
245             {
246               o = Offset (0, Stem::chord_start_f (stem));
247               /*
248                 Default position is on stem X, on outer side of head Y
249                */
250               o += Offset (n->extent (X_AXIS).length ()
251                            * (1 + Stem::get_direction (stem)),
252                            0.5 * ss * Directional_element_interface (me).get ());
253             }
254           else if (str == "stem")
255             {
256               o = Offset (0, Stem::stem_end_position (stem) * hs);
257               /*
258                 Default position is on stem X, at stem end Y
259                */
260               o += Offset (0.5 *
261                            (n->extent (X_AXIS).length ()
262                             - stem->extent (X_AXIS).length ())
263                             * (1 + Stem::get_direction (stem)),
264                             0);
265             }
266         }
267     }
268   else if (str == "loose-end")
269     {
270       SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
271       if (ly_symbol2string (other_a) != "loose-end")
272         {
273 #if 0
274           /*
275             The braindead way: horizontal
276           */
277           o = Offset (0, get_attachment (me, -dir, common)[Y_AXIS]);
278 #else
279           o = broken_trend_offset (me, dir);
280 #endif
281
282           
283         }
284         
285     }
286           
287   SCM l = scm_assoc
288     (scm_listify (a,
289                   gh_int2scm (stem ? Stem::get_direction (stem) : 1 * dir),
290                   gh_int2scm (Directional_element_interface (me).get () * dir),
291                   SCM_UNDEFINED),
292      scm_eval2 (ly_symbol2scm ("slur-extremity-offset-alist"), SCM_EOL));
293   
294   if (l != SCM_BOOL_F)
295     {
296       o += ly_scm2offset (gh_cdr (l)) * ss * dir;
297     }
298
299   /*
300     What if get_bound () is not a note-column?
301    */
302   if (str != "loose-end"
303       && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
304     {      
305       o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS) 
306         - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
307     }
308
309   return o;
310 }
311
312 Offset
313 Slur::encompass_offset (Score_element*me,
314                         Score_element* col,
315                         Score_element **common) 
316 {
317   Offset o;
318   Score_element* stem_l = unsmob_element (col->get_elt_property ("stem"));
319   
320   Direction dir = Directional_element_interface (me).get ();
321   
322   if (!stem_l)
323     {
324       warning (_ ("Slur over rest?"));
325       o[X_AXIS] = col->relative_coordinate (common[X_AXIS], X_AXIS);
326       o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
327       return o;  
328     }
329   Direction stem_dir = Directional_element_interface (stem_l).get ();
330   o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
331
332   /*
333     Simply set x to middle of notehead
334    */
335
336   o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
337
338   if ((stem_dir == dir)
339       && !stem_l->extent (Y_AXIS).empty_b ())
340     {
341       o[Y_AXIS] = stem_l->relative_coordinate (common[Y_AXIS], Y_AXIS); // iuhg
342       o[Y_AXIS] += stem_l->extent (Y_AXIS)[dir];
343     }
344   else
345     {
346       o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);    // ugh
347       o[Y_AXIS] += col->extent (Y_AXIS)[dir];
348     }
349
350   /*
351    leave a gap: slur mustn't touch head/stem
352    */
353   o[Y_AXIS] += dir * me->paper_l ()->get_var ("slur_y_free");
354   return o;
355 }
356
357 Array<Offset>
358 Slur::get_encompass_offset_arr (Score_element*me) 
359 {
360   Spanner*sp = dynamic_cast<Spanner*>(me);
361   SCM eltlist = me->get_elt_property ("note-columns");
362   Score_element *common[] = {me->common_refpoint (eltlist,X_AXIS),
363                              me->common_refpoint (eltlist,Y_AXIS)};
364
365
366   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
367   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
368   
369   Link_array<Score_element>  encompass_arr;
370   while (gh_pair_p (eltlist))
371     {
372       encompass_arr.push (unsmob_element (gh_car (eltlist)));      
373       eltlist =gh_cdr (eltlist);
374     }
375   encompass_arr.reverse ();
376
377   
378   Array<Offset> offset_arr;
379
380   Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
381                  me->relative_coordinate (common[Y_AXIS], Y_AXIS)); 
382
383   int first = 1;
384   int last = encompass_arr.size () - 2;
385
386   offset_arr.push (get_attachment (me, LEFT, common));
387
388   /*
389     left is broken edge
390   */
391
392   if (encompass_arr[0] != sp->get_bound (LEFT))
393     {
394       first--;
395
396       // ?
397       offset_arr[0][Y_AXIS] -=
398         encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS) 
399         - me->relative_coordinate (common[Y_AXIS], Y_AXIS); 
400     }
401
402   /*
403     right is broken edge
404   */
405   if (encompass_arr.top () != sp->get_bound (RIGHT))
406     {
407       last++;
408     }
409
410   for (int i = first; i <= last; i++)
411     {
412       Offset o (encompass_offset (me, encompass_arr[i], common));
413       offset_arr.push (o - origin);
414     }
415
416   offset_arr.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
417
418   if (encompass_arr[0] != sp->get_bound (LEFT))
419     {
420       offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS) 
421         - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
422     }
423
424   return offset_arr;
425 }
426
427
428 MAKE_SCHEME_CALLBACK(Slur,set_spacing_rods);
429 SCM
430 Slur::set_spacing_rods (SCM smob)
431 {
432   Score_element*me = unsmob_element (smob);
433
434   Rod r;
435   Spanner*sp = dynamic_cast<Spanner*>(me);
436   r.item_l_drul_[LEFT] = sp->get_bound (LEFT);
437   r.item_l_drul_[RIGHT] = sp->get_bound (RIGHT);
438   r.distance_f_ =
439     gh_scm2double (me->get_elt_property ("minimum-length"))
440     * me->paper_l ()->get_var ("staffspace");
441
442   r.add_to_cols ();
443   return SCM_UNSPECIFIED;
444 }
445
446
447 /*
448   Ugh should have dash-length + dash-period
449  */
450 MAKE_SCHEME_CALLBACK (Slur, brew_molecule);
451 SCM
452 Slur::brew_molecule (SCM smob)
453 {
454   Score_element * me = unsmob_element (smob);
455   if (!scm_ilength (me->get_elt_property ("note-columns")))
456     {
457       me->suicide ();
458       return SCM_EOL;
459     }
460
461   Real thick = me->paper_l ()->get_var ("stafflinethickness") *
462     gh_scm2double (me->get_elt_property ("thickness"));
463   Bezier one = get_curve (me);
464
465   Molecule a;
466   SCM d =  me->get_elt_property ("dashed");
467   if (gh_number_p (d))
468     a = me->lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
469   else
470     a = me->lookup_l ()->slur (one, Directional_element_interface (me).get () * thick, thick);
471
472   return a.create_scheme();
473 }
474
475 void
476 Slur::set_control_points (Score_element*me)
477 {
478   Real staff_space = Staff_symbol_referencer::staff_space ((Score_element*)me);  
479   Real h_inf = me->paper_l ()->get_var ("slur_height_limit_factor") *
480     staff_space;
481   Real r_0 = me->paper_l ()->get_var ("slur_ratio");
482   
483   Slur_bezier_bow bb (get_encompass_offset_arr (me),
484                       Directional_element_interface (me).get (),
485                       h_inf, r_0);
486
487   if (bb.fit_factor () > 1.0)
488     {
489       Real length = bb.curve_.control_[3][X_AXIS]; 
490       Real default_height = slur_height (length, h_inf, r_0);
491       bb.minimise_enclosed_area (me->paper_l());
492       
493       Real bff = me->paper_l ()->get_var ("slur_force_blowfit");
494       bb.curve_.control_[1][Y_AXIS] *= bff;
495       bb.curve_.control_[2][Y_AXIS] *= bff;
496       bb.blow_fit ();
497
498       Real sb = me->paper_l ()->get_var ("slur_beautiful");
499       Real beautiful = length * default_height * sb;
500       Real area = bb.enclosed_area_f ();
501       
502       /*
503         Slurs that fit beautifully are not ugly
504       */
505       if (area > beautiful)
506         de_uglyfy (me, &bb, default_height);
507     }
508
509   Bezier b = bb.get_bezier ();
510
511
512   SCM controls = SCM_EOL;
513   for (int i= 4; i--;)
514     {
515       controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
516       /*
517         BRRR WHURG.
518         All these null control-points, where do they all come from?
519       */
520       if (i && b.control_[i][X_AXIS] == 0)
521         me->suicide ();
522     }
523
524   me->set_elt_property ("control-points", controls);
525 }
526   
527 Bezier
528 Slur::get_curve (Score_element*me) 
529 {
530   Bezier b;
531   int i = 0;
532
533   if (!Directional_element_interface (me).get ()
534       || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), LEFT)))
535     set_extremities (me);
536   
537   if (!gh_pair_p (me->get_elt_property ("control-points")))
538     set_control_points (me);
539
540   for (SCM s= me->get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
541     {
542       b.control_[i] = ly_scm2offset (gh_car (s));
543       i++;
544     }
545
546   Array<Offset> enc (get_encompass_offset_arr (me));
547   Direction dir = Directional_element_interface (me).get ();
548   
549   Real x1 = enc[0][X_AXIS];
550   Real x2 = enc.top ()[X_AXIS];
551
552   Real off = 0.0;
553   for (int i=1; i < enc.size ()-1; i++)
554     {
555       Real x = enc[i][X_AXIS];
556       if (x > x1 && x <x2)
557         {
558           Real y = b.get_other_coordinate (X_AXIS, x);
559           off = off >? dir *  (enc[i][Y_AXIS] - y);
560         }
561     }
562   b.translate (Offset (0, dir * off));
563   return b;
564 }
565
566
567 bool
568 Slur::has_interface (Score_element*me)
569 {
570   return me->has_interface (ly_symbol2scm ("slur-interface"));
571 }
572
573