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