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