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