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