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