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