]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
patch::: 1.3.79.jcn1
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7     Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10 /*
11   [TODO]
12     * fix broken interstaff slurs
13     * begin and end should be treated as a/acknowledge Scripts.
14     * broken slur should have uniform trend
15     * smart changing of endings
16     * smart changing of (Y-?)offsets to avoid ugly beziers
17        (along-side-stem)
18  */
19
20 #include "directional-element-interface.hh"
21 #include "group-interface.hh"
22 #include "slur.hh"
23 #include "lookup.hh"
24 #include "paper-def.hh"
25 #include "note-column.hh"
26 #include "stem.hh"
27 #include "paper-column.hh"
28 #include "molecule.hh"
29 #include "debug.hh"
30 #include "slur-bezier-bow.hh"
31 #include "main.hh"
32 #include "group-interface.hh"
33 #include "staff-symbol-referencer.hh"
34 #include "spanner.hh"
35
36
37 void
38 Slur::set_interface (Score_element*me)
39 {
40   me->set_elt_property ("attachment", gh_cons (SCM_BOOL_F, SCM_BOOL_F));
41   me->set_interface (ly_symbol2scm ("slur-interface"));
42 }
43
44 void
45 Slur::add_column (Score_element*me, Score_element*n)
46 {
47   if (!gh_pair_p (n->get_elt_property ("note-heads")))
48     warning (_ ("Putting slur over rest.  Ignoring."));
49   else
50     {
51       Pointer_group_interface (me, "note-columns").add_element (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 (Score_element*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       Real c1 = me->paper_l ()->get_var ("bezier_control1");
71       Real c2 = me->paper_l ()->get_var ("bezier_control2");
72       Real c3 = me->paper_l ()->get_var ("bezier_control3");
73       if (h > c1 * f)
74         {
75           h = c1 * f; 
76         }
77       else if (h > c2 + c3 * ind)
78         {
79           h = c2 + c3 * ind; 
80         }
81       
82       bb->curve_.control_[i][Y_AXIS] = h * length;
83     } 
84
85   bb->curve_.assert_sanity ();
86 }
87
88 Direction
89 Slur::get_default_dir (Score_element*me) 
90 {
91   Link_array<Score_element> encompass_arr =
92     Pointer_group_interface__extract_elements (me, (Score_element*)0, "note-columns");
93   
94   Direction d = DOWN;
95   for (int i=0; i < encompass_arr.size (); i ++) 
96     {
97       if (Note_column::dir (encompass_arr[i]) < 0) 
98         {
99           d = UP;
100           break;
101         }
102     }
103   return d;
104 }
105
106
107
108
109
110 Offset
111 Slur::encompass_offset (Score_element*me,
112                         Score_element* col,
113                         Score_element **common) 
114 {
115   Offset o;
116   Score_element* stem_l = unsmob_element (col->get_elt_property ("stem"));
117   
118   Direction dir = Directional_element_interface (me).get ();
119   
120   if (!stem_l)
121     {
122       warning (_ ("Slur over rest?"));
123       o[X_AXIS] = col->relative_coordinate (common[X_AXIS], X_AXIS);
124       o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
125       return o;  
126     }
127   Direction stem_dir = Directional_element_interface (stem_l).get ();
128   o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
129
130   /*
131     Simply set x to middle of notehead
132    */
133
134   o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
135
136   if ((stem_dir == dir)
137       && !stem_l->extent (Y_AXIS).empty_b ())
138     {
139       o[Y_AXIS] = stem_l->relative_coordinate (common[Y_AXIS], Y_AXIS); // iuhg
140     }
141   else
142     {
143       o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);    // ugh
144     }
145
146   /*
147    leave a gap: slur mustn't touch head/stem
148    */
149   o[Y_AXIS] += dir * me->paper_l ()->get_var ("slur_y_free");
150   return o;
151 }
152
153 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking);
154 SCM
155 Slur::after_line_breaking (SCM smob)
156 {
157   Score_element *me = unsmob_element (smob);
158   if (!gh_scm2int(scm_length (me->get_elt_property ("note-columns"))))
159     {
160       me->suicide ();
161       return SCM_UNSPECIFIED;
162     }
163   set_extremities (me);
164   set_control_points (me);
165   return SCM_UNSPECIFIED;
166
167
168 void
169 Slur::set_extremities (Score_element*me)
170 {
171   if (!Directional_element_interface (me).get ())
172     Directional_element_interface (me).set (get_default_dir (me));
173
174   Direction dir = LEFT;
175   do 
176     {
177       if (!gh_symbol_p (index_cell (me->get_elt_property ("attachment"), dir)))
178         {
179           
180           // for (SCM s = get_elt_property ("slur-extremity-rules"); s != SCM_EOL; s = gh_cdr (s))
181           for (SCM s = scm_eval (ly_symbol2scm ("slur-extremity-rules"));
182                s != SCM_EOL; s = gh_cdr (s))
183             {
184               SCM r = gh_call2 (gh_caar (s), me->self_scm (),
185                                  gh_int2scm ((int)dir));
186               if (r != SCM_BOOL_F)
187                 {
188                   index_set_cell (me->get_elt_property ("attachment"), dir,
189                                   gh_cdar (s));
190                   break;
191                 }
192             }
193         }
194     }
195   while (flip (&dir) != LEFT);
196 }
197
198 Offset
199 Slur::get_attachment (Score_element*me,Direction dir,
200                       Score_element **common) 
201 {
202   Spanner*sp = dynamic_cast<Spanner*>(me);
203   SCM s = me->get_elt_property ("attachment");
204   SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
205   String str = ly_symbol2string (a);
206   Real ss = Staff_symbol_referencer::staff_space ((Score_element*)me);
207   Real hs = ss / 2.0;
208   Offset o;
209
210
211   if (Note_column::has_interface (sp->get_bound (dir)))
212     {
213       Score_element * n =sp->get_bound (dir);
214       if (Score_element*st = Note_column::stem_l (n))
215         {
216
217           if (str == "head")
218             {
219               o = Offset (0, Stem::chord_start_f (st ));
220               /*
221                 Default position is centered in X, on outer side of head Y
222                */
223               o += Offset (0.5 * n->extent (X_AXIS).length (),
224                            0.5 * ss * Directional_element_interface (me).get ());
225             }
226           else if (str == "alongside-stem")
227             {
228               o = Offset (0, Stem::chord_start_f (st ));
229               /*
230                 Default position is on stem X, on outer side of head Y
231                */
232               o += Offset (n->extent (X_AXIS).length ()
233                            * (1 + Stem::get_direction (st )),
234                            0.5 * ss * Directional_element_interface (me).get ());
235             }
236           else if (str == "stem")
237             {
238               o = Offset (0, Stem::stem_end_position (st ) * hs);
239               /*
240                 Default position is on stem X, at stem end Y
241                */
242               o += Offset (0.5 *
243                            (n->extent (X_AXIS).length ()
244                             - st->extent (X_AXIS).length ())
245                             * (1 + Stem::get_direction (st )),
246                             0);
247             }
248           else if (str == "loose-end")
249             {
250               SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
251               if (ly_symbol2string (other_a) != "loose-end")
252                 {
253                   o = Offset (0, get_attachment (me, -dir, common)[Y_AXIS]);
254                 }
255             }
256
257           
258           SCM l = scm_assoc
259             (scm_listify (a,
260                           gh_int2scm (Stem::get_direction (st ) * dir),
261                           gh_int2scm (Directional_element_interface (me).get () * dir),
262                           SCM_UNDEFINED),
263              scm_eval (ly_symbol2scm ("slur-extremity-offset-alist")));
264           
265           if (l != SCM_BOOL_F)
266             {
267               o += ly_scm2offset (gh_cdr (l)) * ss * dir;
268             }
269         }
270     }
271
272
273   /*
274     What if get_bound () is not a note-column?
275    */
276   if (str != "loose-end"
277       && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
278     {      
279       o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS) 
280         - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
281     }
282   return o;
283 }
284
285 Array<Offset>
286 Slur::get_encompass_offset_arr (Score_element*me) 
287 {
288   Spanner*sp = dynamic_cast<Spanner*>(me);
289   SCM eltlist = me->get_elt_property ("note-columns");
290   Score_element *common[] = {me->common_refpoint (eltlist,X_AXIS),
291                              me->common_refpoint (eltlist,Y_AXIS)};
292
293
294   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
295   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
296   
297   Link_array<Score_element>  encompass_arr;
298   while (gh_pair_p (eltlist))
299     {
300       encompass_arr.push (unsmob_element (gh_car (eltlist)));      
301       eltlist =gh_cdr (eltlist);
302     }
303   encompass_arr.reverse ();
304
305   
306   Array<Offset> offset_arr;
307
308   Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
309                  me->relative_coordinate (common[Y_AXIS], Y_AXIS)); 
310
311   int first = 1;
312   int last = encompass_arr.size () - 2;
313
314   offset_arr.push (get_attachment (me, LEFT, common));
315
316   /*
317     left is broken edge
318   */
319
320   if (encompass_arr[0] != sp->get_bound (LEFT))
321     {
322       first--;
323
324       // ?
325       offset_arr[0][Y_AXIS] -=
326         encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS) 
327         - me->relative_coordinate (common[Y_AXIS], Y_AXIS); 
328     }
329
330   /*
331     right is broken edge
332   */
333   if (encompass_arr.top () != sp->get_bound (RIGHT))
334     {
335       last++;
336     }
337
338   for (int i = first; i <= last; i++)
339     {
340       Offset o (encompass_offset (me, encompass_arr[i], common));
341       offset_arr.push (o - origin);
342     }
343
344   offset_arr.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
345
346   if (encompass_arr[0] != sp->get_bound (LEFT))
347     {
348       offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS) 
349         - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
350     }
351
352   return offset_arr;
353 }
354
355
356 MAKE_SCHEME_CALLBACK(Slur,set_spacing_rods);
357 SCM
358 Slur::set_spacing_rods (SCM smob)
359 {
360   Score_element*me = unsmob_element (smob);
361
362   Rod r;
363   Spanner*sp = dynamic_cast<Spanner*>(me);
364   r.item_l_drul_[LEFT] = sp->get_bound (LEFT);
365   r.item_l_drul_[RIGHT] = sp->get_bound (RIGHT);
366   r.distance_f_ =
367     gh_scm2double (me->get_elt_property ("minimum-length"))
368     * me->paper_l ()->get_var ("staffspace");
369
370   r.add_to_cols ();
371   return SCM_UNSPECIFIED;
372 }
373
374
375 /*
376   Ugh should have dash-length + dash-period
377  */
378 MAKE_SCHEME_CALLBACK (Slur, brew_molecule);
379 SCM
380 Slur::brew_molecule (SCM smob)
381 {
382   Score_element * me = unsmob_element (smob);
383   if (!gh_scm2int(scm_length (me->get_elt_property ("note-columns"))))
384     {
385       me->suicide ();
386       return SCM_EOL;
387     }
388
389   Real thick = me->paper_l ()->get_var ("stafflinethickness") *
390     gh_scm2double (me->get_elt_property ("thickness"));
391   Bezier one = get_curve (me);
392
393   Molecule a;
394   SCM d =  me->get_elt_property ("dashed");
395   if (gh_number_p (d))
396     a = me->lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
397   else
398     a = me->lookup_l ()->slur (one, Directional_element_interface (me).get () * thick, thick);
399
400   return a.create_scheme();
401 }
402
403 void
404 Slur::set_control_points (Score_element*me)
405 {
406   Real staff_space = Staff_symbol_referencer::staff_space ((Score_element*)me);  
407   Real h_inf = me->paper_l ()->get_var ("slur_height_limit_factor") *
408     staff_space;
409   Real r_0 = me->paper_l ()->get_var ("slur_ratio");
410   
411   Slur_bezier_bow bb (get_encompass_offset_arr (me),
412                       Directional_element_interface (me).get (),
413                       h_inf, r_0);
414
415   if (bb.fit_factor () > 1.0)
416     {
417       Real length = bb.curve_.control_[3][X_AXIS]; 
418       Real default_height = slur_height (length, h_inf, r_0);
419       bb.minimise_enclosed_area (me->paper_l());
420       
421       Real bff = me->paper_l ()->get_var ("slur_force_blowfit");
422       bb.curve_.control_[1][Y_AXIS] *= bff;
423       bb.curve_.control_[2][Y_AXIS] *= bff;
424       bb.blow_fit ();
425
426       Real sb = me->paper_l ()->get_var ("slur_beautiful");
427       Real beautiful = length * default_height * sb;
428       Real area = bb.enclosed_area_f ();
429       
430       /*
431         Slurs that fit beautifully are not ugly
432       */
433       if (area > beautiful)
434         de_uglyfy (me, &bb, default_height);
435     }
436
437   Bezier b = bb.get_bezier ();
438
439
440   SCM controls = SCM_EOL;
441   for (int i= 4; i--;)
442     controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
443
444   me->set_elt_property ("control-points", controls);
445 }
446   
447   
448 Bezier
449 Slur::get_curve (Score_element*me) 
450 {
451   Bezier b;
452   int i = 0;
453
454   if (!Directional_element_interface (me).get ()
455       || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), LEFT)))
456     set_extremities (me);
457   
458   if (!gh_pair_p (me->get_elt_property ("control-points")))
459     set_control_points (me);
460   
461   
462   for (SCM s= me->get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
463     {
464       b.control_[i] = ly_scm2offset (gh_car (s));
465       i++;
466     }
467   
468   Array<Offset> enc (get_encompass_offset_arr (me));
469   Direction dir = Directional_element_interface (me).get ();
470   
471   Real x1 = enc[0][X_AXIS];
472   Real x2 = enc.top ()[X_AXIS];
473   
474   Real off = 0.0;
475   for (int i=1; i < enc.size ()-1; i++)
476     {
477       Real x = enc[i][X_AXIS];
478       if (x > x1 && x <x2)
479         {
480           Real y = b.get_other_coordinate (X_AXIS, x);
481           off = off >? dir *  (enc[i][Y_AXIS] - y);
482         }
483     }
484   b.translate (Offset (0, dir * off));
485   return b;
486 }
487
488
489 bool
490 Slur::has_interface (Score_element*me)
491 {
492   return me->has_interface (ly_symbol2scm ("slur-interface"));
493 }
494
495