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