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