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