2 slur.cc -- implement Slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
12 * begin and end should be treated as a/acknowledge Scripts.
13 * broken slur should have uniform trend
16 #include "directional-element-interface.hh"
17 #include "group-interface.hh"
20 #include "paper-def.hh"
21 #include "note-column.hh"
23 #include "paper-column.hh"
24 #include "molecule.hh"
26 #include "slur-bezier-bow.hh"
28 #include "cross-staff.hh"
29 #include "group-interface.hh"
30 #include "staff-symbol-referencer.hh"
36 silly value for testing
38 set_elt_property ("attachment", gh_cons (ly_symbol2scm ("alongside-stem"),
39 ly_symbol2scm ("alongside-stem")));
40 set_elt_pointer ("note-columns", SCM_EOL);
41 set_elt_property ("control-points", SCM_EOL);
45 Slur::add_column (Note_column*n)
47 if (!gh_pair_p (n->get_elt_pointer ("note-heads")))
48 warning (_ ("Putting slur over rest. Ignoring."));
51 Pointer_group_interface (this, "note-columns").add_element (n);
57 Slur::de_uglyfy (Slur_bezier_bow* bb, Real default_height)
59 Real length = bb->curve_.control_[3][X_AXIS] ;
60 Real ff = bb->fit_factor ();
61 for (int i = 1; i < 3; i++)
63 Real ind = abs (bb->curve_.control_[(i-1)*3][X_AXIS]
64 - bb->curve_.control_[i][X_AXIS]) / length;
65 Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
67 Real f = default_height / length;
68 Real c1 = paper_l ()->get_var ("bezier_control1");
69 Real c2 = paper_l ()->get_var ("bezier_control2");
70 Real c3 = paper_l ()->get_var ("bezier_control3");
75 else if (h > c2 + c3 * ind)
80 bb->curve_.control_[i][Y_AXIS] = h * length;
83 bb->curve_.assert_sanity ();
87 Slur::get_default_dir () const
89 Link_array<Note_column> encompass_arr =
90 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
93 for (int i=0; i < encompass_arr.size (); i ++)
95 if (encompass_arr[i]->dir () < 0)
105 Slur::do_add_processing ()
107 Link_array<Note_column> encompass_arr =
108 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
110 if (encompass_arr.size ())
112 set_bound (LEFT, encompass_arr[0]);
113 if (encompass_arr.size () > 1)
114 set_bound (RIGHT, encompass_arr.top ());
121 Slur::encompass_offset (Note_column const* col) const
124 Stem* stem_l = col->stem_l ();
125 Direction dir = directional_element (this).get ();
129 warning (_ ("Slur over rest?"));
130 o[X_AXIS] = col->relative_coordinate (0, X_AXIS);
131 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
134 Direction stem_dir = directional_element (stem_l).get ();
135 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
138 Simply set x to middle of notehead
141 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
143 if ((stem_dir == dir)
144 && !stem_l->extent (Y_AXIS).empty_b ())
146 o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
150 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
154 leave a gap: slur mustn't touch head/stem
156 o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
157 o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
161 GLUE_SCORE_ELEMENT(Slur,after_line_breaking);
164 Slur::member_after_line_breaking ()
167 set_control_points ();
168 return SCM_UNDEFINED;
172 slur_get_bound (SCM slur, SCM dir)
174 return ((Slur*)unsmob_element (slur))->get_bound (to_dir (dir))->self_scm_;
178 score_element_get_pointer (SCM se, SCM name)
180 SCM s = scm_assq (name, unsmob_element (se)->pointer_alist_);
181 return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s);
185 score_element_get_property (SCM se, SCM name)
187 SCM s = scm_assq (name, unsmob_element (se)->property_alist_);
188 return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s);
194 scm_make_gsubr ("get-pointer", 2 , 0, 0,
195 (SCM(*)(...)) score_element_get_pointer);
196 scm_make_gsubr ("get-property", 2 , 0, 0,
197 (SCM(*)(...)) score_element_get_property);
198 scm_make_gsubr ("get-bound", 2 , 0, 0,
199 (SCM(*)(...)) slur_get_bound);
202 ADD_SCM_INIT_FUNC (score_elt, init_score_elts);
205 Slur::set_extremities ()
207 if (!directional_element (this).get ())
208 directional_element (this).set (get_default_dir ());
210 Direction dir = LEFT;
213 // for (SCM s = get_elt_property ("slur-extremity-rules"); s != SCM_EOL; s = gh_cdr (s))
214 for (SCM s = scm_eval (ly_symbol2scm ("slur-extremity-rules"));
215 s != SCM_EOL; s = gh_cdr (s))
217 SCM r = scm_eval (scm_listify (gh_caar (s),
219 gh_int2scm ((int)dir),
223 index_set_cell (get_elt_property ("attachment"), dir,
229 while (flip (&dir) != LEFT);
233 Slur::get_attachment (Direction dir) const
235 SCM s = get_elt_property ("attachment");
236 SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
237 Real ss = Staff_symbol_referencer_interface (this).staff_space ();
240 if (Note_column* n = dynamic_cast<Note_column*> (get_bound (dir)))
242 if (Stem* st = dynamic_cast<Stem*> (n->stem_l ()))
244 String str = ly_symbol2string (a);
247 o = Offset (0, st->chord_start_f ());
249 else if (str == "alongside-stem")
251 o = Offset (0, st->chord_start_f ());
253 else if (str == "stem")
255 o = Offset (0, st->stem_end_position () * hs);
257 else if (str == "loose-end")
259 SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
260 if (ly_symbol2string (other_a) != "loose-end")
262 o = Offset (0, get_attachment (-dir)[Y_AXIS]);
266 o += Offset (0.5 * st->get_direction ()
267 * n->extent (X_AXIS).length (), 0);
271 gh_int2scm (st->get_direction () * dir),
272 gh_int2scm (directional_element (this).get () * dir),
274 scm_eval (ly_symbol2scm ("slur-extremity-offset-alist")));
278 o += ly_scm2offset (gh_cdr (l)) * ss * dir;
287 Slur::cross_staff_count ()const
289 Link_array<Note_column> encompass_arr =
290 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
294 for (int i = 0; i < encompass_arr.size (); i++)
296 if (calc_interstaff_dist (encompass_arr[i], this))
304 Slur::get_encompass_offset_arr () const
306 Link_array<Note_column> encompass_arr =
307 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
309 Array<Offset> offset_arr;
311 Offset origin (relative_coordinate (0, X_AXIS), 0);
314 int last = encompass_arr.size () - 2;
316 offset_arr.push (get_attachment (LEFT));
321 int cross_count = cross_staff_count ();
322 bool cross_b = cross_count && cross_count < encompass_arr.size ();
323 if (encompass_arr[0] != get_bound (LEFT))
326 Real is = calc_interstaff_dist (encompass_arr[0], this);
328 offset_arr[0][Y_AXIS] += is;
334 if (encompass_arr.top () != get_bound (RIGHT))
339 for (int i = first; i <= last; i++)
341 Offset o (encompass_offset (encompass_arr[i]));
342 offset_arr.push (o - origin);
345 offset_arr.push (Offset (spanner_length (), 0) + get_attachment (RIGHT));
352 Slur::get_rods () const
357 r.item_l_drul_[LEFT] = get_bound (LEFT);
358 r.item_l_drul_[RIGHT] = get_bound (RIGHT);
359 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
367 Ugh should have dash-length + dash-period
369 GLUE_SCORE_ELEMENT(Slur,brew_molecule);
371 Slur::member_brew_molecule () const
373 Real thick = paper_l ()->get_var ("slur_thickness");
374 Bezier one = get_curve ();
377 SCM d = get_elt_property ("dashed");
379 a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
381 a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
383 return a.create_scheme();
387 Slur::set_control_points ()
389 Slur_bezier_bow bb (get_encompass_offset_arr (),
390 directional_element (this).get ());
392 Real staff_space = Staff_symbol_referencer_interface (this).staff_space ();
393 Real h_inf = paper_l ()->get_var ("slur_height_limit_factor") * staff_space;
394 Real r_0 = paper_l ()->get_var ("slur_ratio");
396 bb.set_default_bezier (h_inf, r_0);
398 if (bb.fit_factor () > 1.0)
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);
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;
409 Real sb = paper_l ()->get_var ("slur_beautiful");
410 Real beautiful = length * default_height * sb;
411 Real area = bb.enclosed_area_f ();
414 Slurs that fit beautifully are not ugly
416 if (area > beautiful)
417 de_uglyfy (&bb, default_height);
420 Bezier b = bb.get_bezier ();
423 SCM controls = SCM_EOL;
425 controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
427 set_elt_property ("control-points", controls);
432 Slur::get_curve () const
437 if (!directional_element (this).get ())
438 ((Slur*)this)->set_extremities ();
440 if (!gh_pair_p (get_elt_property ("control-points")))
441 ((Slur*)this)->set_control_points ();
444 for (SCM s= get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
446 b.control_[i] = ly_scm2offset (gh_car (s));
450 Array<Offset> enc (get_encompass_offset_arr ());
451 Direction dir = directional_element (this).get ();
453 Real x1 = enc[0][X_AXIS];
454 Real x2 = enc.top ()[X_AXIS];
457 for (int i=1; i < enc.size ()-1; i++)
459 Real x = enc[i][X_AXIS];
462 Real y = b.get_other_coordinate (X_AXIS, x);
463 off = off >? dir * (enc[i][Y_AXIS] - y);
466 b.translate (Offset (0, dir * off));