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