]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur.cc
patch::: 1.3.79.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--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       o[Y_AXIS] += stem_l->extent (Y_AXIS)[dir];
141     }
142   else
143     {
144       o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);    // ugh
145       o[Y_AXIS] += col->extent (Y_AXIS)[dir];
146     }
147
148   /*
149    leave a gap: slur mustn't touch head/stem
150    */
151   o[Y_AXIS] += dir * me->paper_l ()->get_var ("slur_y_free");
152   return o;
153 }
154
155 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking);
156 SCM
157 Slur::after_line_breaking (SCM smob)
158 {
159   Score_element *me = unsmob_element (smob);
160   if (!gh_scm2int(scm_length (me->get_elt_property ("note-columns"))))
161     {
162       me->suicide ();
163       return SCM_UNSPECIFIED;
164     }
165   set_extremities (me);
166   set_control_points (me);
167   return SCM_UNSPECIFIED;
168
169
170 void
171 Slur::set_extremities (Score_element*me)
172 {
173   if (!Directional_element_interface (me).get ())
174     Directional_element_interface (me).set (get_default_dir (me));
175
176   Direction dir = LEFT;
177   do 
178     {
179       if (!gh_symbol_p (index_cell (me->get_elt_property ("attachment"), dir)))
180         {
181           
182           // for (SCM s = get_elt_property ("slur-extremity-rules"); s != SCM_EOL; s = gh_cdr (s))
183           for (SCM s = scm_eval (ly_symbol2scm ("slur-extremity-rules"));
184                s != SCM_EOL; s = gh_cdr (s))
185             {
186               SCM r = gh_call2 (gh_caar (s), me->self_scm (),
187                                  gh_int2scm ((int)dir));
188               if (r != SCM_BOOL_F)
189                 {
190                   index_set_cell (me->get_elt_property ("attachment"), dir,
191                                   gh_cdar (s));
192                   break;
193                 }
194             }
195         }
196     }
197   while (flip (&dir) != LEFT);
198 }
199
200 Offset
201 Slur::get_attachment (Score_element*me,Direction dir,
202                       Score_element **common) 
203 {
204   Spanner*sp = dynamic_cast<Spanner*>(me);
205   SCM s = me->get_elt_property ("attachment");
206   SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
207   String str = ly_symbol2string (a);
208   Real ss = Staff_symbol_referencer::staff_space ((Score_element*)me);
209   Real hs = ss / 2.0;
210   Offset o;
211
212   if (Note_column::has_interface (sp->get_bound (dir)))
213     {
214       Score_element * n =sp->get_bound (dir);
215       if (Score_element*st = Note_column::stem_l (n))
216         {
217
218           if (str == "head")
219             {
220               o = Offset (0, Stem::chord_start_f (st ));
221               /*
222                 Default position is centered in X, on outer side of head Y
223                */
224               o += Offset (0.5 * n->extent (X_AXIS).length (),
225                            0.5 * ss * Directional_element_interface (me).get ());
226             }
227           else if (str == "alongside-stem")
228             {
229               o = Offset (0, Stem::chord_start_f (st ));
230               /*
231                 Default position is on stem X, on outer side of head Y
232                */
233               o += Offset (n->extent (X_AXIS).length ()
234                            * (1 + Stem::get_direction (st )),
235                            0.5 * ss * Directional_element_interface (me).get ());
236             }
237           else if (str == "stem")
238             {
239               o = Offset (0, Stem::stem_end_position (st ) * hs);
240               /*
241                 Default position is on stem X, at stem end Y
242                */
243               o += Offset (0.5 *
244                            (n->extent (X_AXIS).length ()
245                             - st->extent (X_AXIS).length ())
246                             * (1 + Stem::get_direction (st )),
247                             0);
248             }
249           else if (str == "loose-end")
250             {
251               SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
252               if (ly_symbol2string (other_a) != "loose-end")
253                 {
254                   o = Offset (0, get_attachment (me, -dir, common)[Y_AXIS]);
255                 }
256               else if (dir == RIGHT)
257                 {
258                   o[X_AXIS] = (sp->get_bound (dir)->relative_coordinate (common[X_AXIS], X_AXIS) 
259                                - me->relative_coordinate (common[X_AXIS], X_AXIS));
260                 }
261             }
262
263           
264           SCM l = scm_assoc
265             (scm_listify (a,
266                           gh_int2scm (Stem::get_direction (st ) * dir),
267                           gh_int2scm (Directional_element_interface (me).get () * dir),
268                           SCM_UNDEFINED),
269              scm_eval (ly_symbol2scm ("slur-extremity-offset-alist")));
270           
271           if (l != SCM_BOOL_F)
272             {
273               o += ly_scm2offset (gh_cdr (l)) * ss * dir;
274             }
275         }
276     }
277
278
279   /*
280     What if get_bound () is not a note-column?
281    */
282   if (str != "loose-end"
283       && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
284     {      
285       o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS) 
286         - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
287     }
288
289   return o;
290 }
291
292 Array<Offset>
293 Slur::get_encompass_offset_arr (Score_element*me) 
294 {
295   Spanner*sp = dynamic_cast<Spanner*>(me);
296   SCM eltlist = me->get_elt_property ("note-columns");
297   Score_element *common[] = {me->common_refpoint (eltlist,X_AXIS),
298                              me->common_refpoint (eltlist,Y_AXIS)};
299
300
301   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
302   common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
303   
304   Link_array<Score_element>  encompass_arr;
305   while (gh_pair_p (eltlist))
306     {
307       encompass_arr.push (unsmob_element (gh_car (eltlist)));      
308       eltlist =gh_cdr (eltlist);
309     }
310   encompass_arr.reverse ();
311
312   
313   Array<Offset> offset_arr;
314
315   Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
316                  me->relative_coordinate (common[Y_AXIS], Y_AXIS)); 
317
318   int first = 1;
319   int last = encompass_arr.size () - 2;
320
321   offset_arr.push (get_attachment (me, LEFT, common));
322
323   /*
324     left is broken edge
325   */
326
327   if (encompass_arr[0] != sp->get_bound (LEFT))
328     {
329       first--;
330
331       // ?
332       offset_arr[0][Y_AXIS] -=
333         encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS) 
334         - me->relative_coordinate (common[Y_AXIS], Y_AXIS); 
335     }
336
337   /*
338     right is broken edge
339   */
340   if (encompass_arr.top () != sp->get_bound (RIGHT))
341     {
342       last++;
343     }
344
345   for (int i = first; i <= last; i++)
346     {
347       Offset o (encompass_offset (me, encompass_arr[i], common));
348       offset_arr.push (o - origin);
349     }
350
351   offset_arr.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
352
353   if (encompass_arr[0] != sp->get_bound (LEFT))
354     {
355       offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS) 
356         - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
357     }
358
359   return offset_arr;
360 }
361
362
363 MAKE_SCHEME_CALLBACK(Slur,set_spacing_rods);
364 SCM
365 Slur::set_spacing_rods (SCM smob)
366 {
367   Score_element*me = unsmob_element (smob);
368
369   Rod r;
370   Spanner*sp = dynamic_cast<Spanner*>(me);
371   r.item_l_drul_[LEFT] = sp->get_bound (LEFT);
372   r.item_l_drul_[RIGHT] = sp->get_bound (RIGHT);
373   r.distance_f_ =
374     gh_scm2double (me->get_elt_property ("minimum-length"))
375     * me->paper_l ()->get_var ("staffspace");
376
377   r.add_to_cols ();
378   return SCM_UNSPECIFIED;
379 }
380
381
382 /*
383   Ugh should have dash-length + dash-period
384  */
385 MAKE_SCHEME_CALLBACK (Slur, brew_molecule);
386 SCM
387 Slur::brew_molecule (SCM smob)
388 {
389   Score_element * me = unsmob_element (smob);
390   if (!gh_scm2int(scm_length (me->get_elt_property ("note-columns"))))
391     {
392       me->suicide ();
393       return SCM_EOL;
394     }
395
396   Real thick = me->paper_l ()->get_var ("stafflinethickness") *
397     gh_scm2double (me->get_elt_property ("thickness"));
398   Bezier one = get_curve (me);
399
400   Molecule a;
401   SCM d =  me->get_elt_property ("dashed");
402   if (gh_number_p (d))
403     a = me->lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
404   else
405     a = me->lookup_l ()->slur (one, Directional_element_interface (me).get () * thick, thick);
406
407   return a.create_scheme();
408 }
409
410 void
411 Slur::set_control_points (Score_element*me)
412 {
413   Real staff_space = Staff_symbol_referencer::staff_space ((Score_element*)me);  
414   Real h_inf = me->paper_l ()->get_var ("slur_height_limit_factor") *
415     staff_space;
416   Real r_0 = me->paper_l ()->get_var ("slur_ratio");
417   
418   Slur_bezier_bow bb (get_encompass_offset_arr (me),
419                       Directional_element_interface (me).get (),
420                       h_inf, r_0);
421
422   if (bb.fit_factor () > 1.0)
423     {
424       Real length = bb.curve_.control_[3][X_AXIS]; 
425       Real default_height = slur_height (length, h_inf, r_0);
426       bb.minimise_enclosed_area (me->paper_l());
427       
428       Real bff = me->paper_l ()->get_var ("slur_force_blowfit");
429       bb.curve_.control_[1][Y_AXIS] *= bff;
430       bb.curve_.control_[2][Y_AXIS] *= bff;
431       bb.blow_fit ();
432
433       Real sb = me->paper_l ()->get_var ("slur_beautiful");
434       Real beautiful = length * default_height * sb;
435       Real area = bb.enclosed_area_f ();
436       
437       /*
438         Slurs that fit beautifully are not ugly
439       */
440       if (area > beautiful)
441         de_uglyfy (me, &bb, default_height);
442     }
443
444   Bezier b = bb.get_bezier ();
445
446
447   SCM controls = SCM_EOL;
448   for (int i= 4; i--;)
449     {
450       controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
451       /*
452         BRRR WHURG.
453         All these null control-points, where do they all come from?
454       */
455       if (i && b.control_[i][X_AXIS] == 0)
456         me->suicide ();
457     }
458
459   me->set_elt_property ("control-points", controls);
460 }
461   
462 Bezier
463 Slur::get_curve (Score_element*me) 
464 {
465   Bezier b;
466   int i = 0;
467
468   if (!Directional_element_interface (me).get ()
469       || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), LEFT)))
470     set_extremities (me);
471   
472   if (!gh_pair_p (me->get_elt_property ("control-points")))
473     set_control_points (me);
474
475   for (SCM s= me->get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
476     {
477       b.control_[i] = ly_scm2offset (gh_car (s));
478       i++;
479     }
480
481   Array<Offset> enc (get_encompass_offset_arr (me));
482   Direction dir = Directional_element_interface (me).get ();
483   
484   Real x1 = enc[0][X_AXIS];
485   Real x2 = enc.top ()[X_AXIS];
486
487   Real off = 0.0;
488   for (int i=1; i < enc.size ()-1; i++)
489     {
490       Real x = enc[i][X_AXIS];
491       if (x > x1 && x <x2)
492         {
493           Real y = b.get_other_coordinate (X_AXIS, x);
494           off = off >? dir *  (enc[i][Y_AXIS] - y);
495         }
496     }
497   b.translate (Offset (0, dir * off));
498   return b;
499 }
500
501
502 bool
503 Slur::has_interface (Score_element*me)
504 {
505   return me->has_interface (ly_symbol2scm ("slur-interface"));
506 }
507
508