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
14 * smart changing of endings
15 * smart changing of (Y-?)offsets to avoid ugly beziers
19 #include "directional-element-interface.hh"
20 #include "group-interface.hh"
23 #include "paper-def.hh"
24 #include "note-column.hh"
26 #include "paper-column.hh"
27 #include "molecule.hh"
29 #include "slur-bezier-bow.hh"
31 #include "cross-staff.hh"
32 #include "group-interface.hh"
33 #include "staff-symbol-referencer.hh"
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);
44 Slur::add_column (Note_column*n)
46 if (!gh_pair_p (n->get_elt_pointer ("note-heads")))
47 warning (_ ("Putting slur over rest. Ignoring."));
50 Pointer_group_interface (this, "note-columns").add_element (n);
54 add_bound_item (this, n);
58 Slur::de_uglyfy (Slur_bezier_bow* bb, Real default_height)
60 Real length = bb->curve_.control_[3][X_AXIS] ;
61 Real ff = bb->fit_factor ();
62 for (int i = 1; i < 3; i++)
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;
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");
76 else if (h > c2 + c3 * ind)
81 bb->curve_.control_[i][Y_AXIS] = h * length;
84 bb->curve_.assert_sanity ();
88 Slur::get_default_dir () const
90 Link_array<Note_column> encompass_arr =
91 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
94 for (int i=0; i < encompass_arr.size (); i ++)
96 if (encompass_arr[i]->dir () < 0)
106 Slur::do_add_processing ()
109 Link_array<Note_column> encompass_arr =
110 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
112 if (encompass_arr.size ())
114 set_bound (LEFT, encompass_arr[0]);
115 if (encompass_arr.size () > 1)
116 set_bound (RIGHT, encompass_arr.top ());
124 Slur::encompass_offset (Note_column const* col) const
127 Stem* stem_l = col->stem_l ();
128 Direction dir = Directional_element_interface (this).get ();
132 warning (_ ("Slur over rest?"));
133 o[X_AXIS] = col->relative_coordinate (0, X_AXIS);
134 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
137 Direction stem_dir = Directional_element_interface (stem_l).get ();
138 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
141 Simply set x to middle of notehead
144 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
146 if ((stem_dir == dir)
147 && !stem_l->extent (Y_AXIS).empty_b ())
149 o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
153 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
157 leave a gap: slur mustn't touch head/stem
159 o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
160 o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
164 GLUE_SCORE_ELEMENT(Slur,after_line_breaking);
167 Slur::member_after_line_breaking ()
170 set_control_points ();
171 return SCM_UNDEFINED;
175 slur_get_bound (SCM slur, SCM dir)
177 return ((Slur*)unsmob_element (slur))->get_bound (to_dir (dir))->self_scm_;
181 score_element_get_pointer (SCM se, SCM name)
183 SCM s = scm_assq (name, unsmob_element (se)->pointer_alist_);
184 return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s);
188 score_element_get_property (SCM se, SCM name)
190 SCM s = scm_assq (name, unsmob_element (se)->property_alist_);
191 return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s);
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);
205 ADD_SCM_INIT_FUNC (score_elt, init_score_elts);
208 Slur::set_extremities ()
210 if (!Directional_element_interface (this).get ())
211 Directional_element_interface (this).set (get_default_dir ());
213 Direction dir = LEFT;
216 if (!gh_symbol_p (index_cell (get_elt_property ("attachment"), dir)))
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))
223 SCM r = scm_eval (scm_listify (gh_caar (s),
225 gh_int2scm ((int)dir),
229 index_set_cell (get_elt_property ("attachment"), dir,
236 while (flip (&dir) != LEFT);
240 Slur::get_attachment (Direction dir) const
242 SCM s = get_elt_property ("attachment");
243 SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
244 String str = ly_symbol2string (a);
245 Real ss = Staff_symbol_referencer_interface (this).staff_space ();
248 if (Note_column* n = dynamic_cast<Note_column*> (get_bound (dir)))
250 if (Stem* st = dynamic_cast<Stem*> (n->stem_l ()))
254 o = Offset (0, st->chord_start_f ());
256 Default position is centered in X, on outer side of head Y
258 o += Offset (0.5 * n->extent (X_AXIS).length (),
259 0.5 * ss * Directional_element_interface (this).get ());
261 else if (str == "alongside-stem")
263 o = Offset (0, st->chord_start_f ());
265 Default position is on stem X, on outer side of head Y
267 o += Offset (n->extent (X_AXIS).length ()
268 * (1 + st->get_direction ()),
269 0.5 * ss * Directional_element_interface (this).get ());
271 else if (str == "stem")
273 o = Offset (0, st->stem_end_position () * hs);
275 Default position is on stem X, at stem end Y
278 (n->extent (X_AXIS).length ()
279 - st->extent (X_AXIS).length ())
280 * (1 + st->get_direction ()),
283 else if (str == "loose-end")
285 SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
286 if (ly_symbol2string (other_a) != "loose-end")
288 o = Offset (0, get_attachment (-dir)[Y_AXIS]);
295 gh_int2scm (st->get_direction () * dir),
296 gh_int2scm (Directional_element_interface (this).get () * dir),
298 scm_eval (ly_symbol2scm ("slur-extremity-offset-alist")));
302 o += ly_scm2offset (gh_cdr (l)) * ss * dir;
312 if (str != "loose-end")
314 Link_array<Note_column> encompass_arr =
315 Pointer_group_interface__extract_elements (this, (Note_column*)0,
317 o -= Offset (0, calc_interstaff_dist (dir == LEFT ? encompass_arr[0]
318 : encompass_arr.top (), this));
324 Slur::cross_staff_count ()const
326 Link_array<Note_column> encompass_arr =
327 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
331 for (int i = 0; i < encompass_arr.size (); i++)
333 if (calc_interstaff_dist (encompass_arr[i], this))
341 Slur::get_encompass_offset_arr () const
343 Link_array<Note_column> encompass_arr =
344 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
346 Array<Offset> offset_arr;
348 Offset origin (relative_coordinate (0, X_AXIS), 0);
351 int last = encompass_arr.size () - 2;
353 offset_arr.push (get_attachment (LEFT));
358 int cross_count = cross_staff_count ();
363 bool cross_b = cross_count && cross_count < encompass_arr.size ();
364 if (encompass_arr[0] != get_bound (LEFT))
367 Real is = calc_interstaff_dist (encompass_arr[0], this);
369 offset_arr[0][Y_AXIS] += is;
375 if (encompass_arr.top () != get_bound (RIGHT))
380 for (int i = first; i <= last; i++)
382 Offset o (encompass_offset (encompass_arr[i]));
383 offset_arr.push (o - origin);
386 offset_arr.push (Offset (spanner_length (), 0) + get_attachment (RIGHT));
393 Slur::get_rods () const
398 r.item_l_drul_[LEFT] = get_bound (LEFT);
399 r.item_l_drul_[RIGHT] = get_bound (RIGHT);
400 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
408 Ugh should have dash-length + dash-period
410 GLUE_SCORE_ELEMENT(Slur,brew_molecule);
412 Slur::member_brew_molecule () const
414 Real thick = paper_l ()->get_var ("slur_thickness");
415 Bezier one = get_curve ();
418 SCM d = get_elt_property ("dashed");
420 a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
422 a = lookup_l ()->slur (one, Directional_element_interface (this).get () * thick, thick);
424 return a.create_scheme();
428 Slur::set_control_points ()
430 Slur_bezier_bow bb (get_encompass_offset_arr (),
431 Directional_element_interface (this).get ());
433 Real staff_space = Staff_symbol_referencer_interface (this).staff_space ();
434 Real h_inf = paper_l ()->get_var ("slur_height_limit_factor") * staff_space;
435 Real r_0 = paper_l ()->get_var ("slur_ratio");
437 bb.set_default_bezier (h_inf, r_0);
439 if (bb.fit_factor () > 1.0)
441 Real length = bb.curve_.control_[3][X_AXIS];
442 Real default_height = bb.get_default_height (h_inf, r_0, length);
443 bb.minimise_enclosed_area (paper_l(), default_height);
445 Real bff = paper_l ()->get_var ("slur_force_blowfit");
446 bb.curve_.control_[1][Y_AXIS] *= bff;
447 bb.curve_.control_[2][Y_AXIS] *= bff;
450 Real sb = paper_l ()->get_var ("slur_beautiful");
451 Real beautiful = length * default_height * sb;
452 Real area = bb.enclosed_area_f ();
455 Slurs that fit beautifully are not ugly
457 if (area > beautiful)
458 de_uglyfy (&bb, default_height);
461 Bezier b = bb.get_bezier ();
464 SCM controls = SCM_EOL;
466 controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
468 set_elt_property ("control-points", controls);
473 Slur::get_curve () const
478 if (!Directional_element_interface (this).get ()
479 || ! gh_symbol_p (index_cell (get_elt_property ("attachment"), LEFT)))
480 ((Slur*)this)->set_extremities ();
482 if (!gh_pair_p (get_elt_property ("control-points")))
483 ((Slur*)this)->set_control_points ();
486 for (SCM s= get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
488 b.control_[i] = ly_scm2offset (gh_car (s));
492 Array<Offset> enc (get_encompass_offset_arr ());
493 Direction dir = Directional_element_interface (this).get ();
495 Real x1 = enc[0][X_AXIS];
496 Real x2 = enc.top ()[X_AXIS];
499 for (int i=1; i < enc.size ()-1; i++)
501 Real x = enc[i][X_AXIS];
504 Real y = b.get_other_coordinate (X_AXIS, x);
505 off = off >? dir * (enc[i][Y_AXIS] - y);
508 b.translate (Offset (0, dir * off));