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