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 * 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
20 #include "directional-element-interface.hh"
21 #include "group-interface.hh"
24 #include "paper-def.hh"
25 #include "note-column.hh"
27 #include "paper-column.hh"
28 #include "molecule.hh"
30 #include "slur-bezier-bow.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)
110 Slur::encompass_offset (Score_element* col,
111 Score_element **common) const
114 Score_element* stem_l = unsmob_element (col->get_elt_pointer ("stem"));
116 Direction dir = Directional_element_interface (this).get ();
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);
125 Direction stem_dir = Directional_element_interface (stem_l).get ();
126 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
129 Simply set x to middle of notehead
132 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
134 if ((stem_dir == dir)
135 && !stem_l->extent (Y_AXIS).empty_b ())
137 o[Y_AXIS] = stem_l->relative_coordinate (common[Y_AXIS], Y_AXIS); // iuhg
141 o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS); // ugh
145 leave a gap: slur mustn't touch head/stem
147 o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
151 GLUE_SCORE_ELEMENT(Slur,after_line_breaking);
154 Slur::member_after_line_breaking ()
157 set_control_points ();
158 return SCM_UNDEFINED;
162 Slur::set_extremities ()
164 if (!Directional_element_interface (this).get ())
165 Directional_element_interface (this).set (get_default_dir ());
167 Direction dir = LEFT;
170 if (!gh_symbol_p (index_cell (get_elt_property ("attachment"), dir)))
173 // for (SCM s = get_elt_property ("slur-extremity-rules"); s != SCM_EOL; s = gh_cdr (s))
174 for (SCM s = scm_eval (ly_symbol2scm ("slur-extremity-rules"));
175 s != SCM_EOL; s = gh_cdr (s))
177 SCM r = gh_call2 (gh_caar (s), this->self_scm_,
178 gh_int2scm ((int)dir));
181 index_set_cell (get_elt_property ("attachment"), dir,
188 while (flip (&dir) != LEFT);
192 Slur::get_attachment (Direction dir,
193 Score_element **common) const
195 SCM s = get_elt_property ("attachment");
196 SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
197 String str = ly_symbol2string (a);
198 Real ss = Staff_symbol_referencer_interface (this).staff_space ();
203 if (Note_column* n = dynamic_cast<Note_column*> (get_bound (dir)))
205 if (Stem* st = dynamic_cast<Stem*> (n->stem_l ()))
209 o = Offset (0, st->chord_start_f ());
211 Default position is centered in X, on outer side of head Y
213 o += Offset (0.5 * n->extent (X_AXIS).length (),
214 0.5 * ss * Directional_element_interface (this).get ());
216 else if (str == "alongside-stem")
218 o = Offset (0, st->chord_start_f ());
220 Default position is on stem X, on outer side of head Y
222 o += Offset (n->extent (X_AXIS).length ()
223 * (1 + st->get_direction ()),
224 0.5 * ss * Directional_element_interface (this).get ());
226 else if (str == "stem")
228 o = Offset (0, st->stem_end_position () * hs);
230 Default position is on stem X, at stem end Y
233 (n->extent (X_AXIS).length ()
234 - st->extent (X_AXIS).length ())
235 * (1 + st->get_direction ()),
238 else if (str == "loose-end")
240 SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
241 if (ly_symbol2string (other_a) != "loose-end")
243 o = Offset (0, get_attachment (-dir, common)[Y_AXIS]);
250 gh_int2scm (st->get_direction () * dir),
251 gh_int2scm (Directional_element_interface (this).get () * dir),
253 scm_eval (ly_symbol2scm ("slur-extremity-offset-alist")));
257 o += ly_scm2offset (gh_cdr (l)) * ss * dir;
264 What if get_bound () is not a note-column?
266 if (str != "loose-end"
267 && get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
269 o[Y_AXIS] += get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS)
270 - relative_coordinate (common[Y_AXIS], Y_AXIS);
276 Slur::get_encompass_offset_arr () const
278 SCM eltlist = get_elt_pointer ("note-columns");
279 Score_element *common[] = {common_refpoint (eltlist,X_AXIS),
280 common_refpoint (eltlist,Y_AXIS)};
283 common[X_AXIS] = common[X_AXIS]->common_refpoint (get_bound (RIGHT), X_AXIS);
284 common[X_AXIS] = common[X_AXIS]->common_refpoint (get_bound (LEFT), X_AXIS);
286 Link_array<Score_element> encompass_arr;
287 while (gh_pair_p (eltlist))
289 encompass_arr.push (unsmob_element (gh_car (eltlist)));
290 eltlist =gh_cdr (eltlist);
292 encompass_arr.reverse ();
295 Array<Offset> offset_arr;
297 Offset origin (relative_coordinate (common[X_AXIS], X_AXIS),
298 relative_coordinate (common[Y_AXIS], Y_AXIS));
301 int last = encompass_arr.size () - 2;
303 offset_arr.push (get_attachment (LEFT, common));
309 if (encompass_arr[0] != get_bound (LEFT))
314 offset_arr[0][Y_AXIS] -=
315 encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS)
316 - relative_coordinate (common[Y_AXIS], Y_AXIS);
322 if (encompass_arr.top () != get_bound (RIGHT))
327 for (int i = first; i <= last; i++)
329 Offset o (encompass_offset (encompass_arr[i], common));
330 offset_arr.push (o - origin);
333 offset_arr.push (Offset (spanner_length (), 0) + get_attachment (RIGHT,common));
335 if (encompass_arr[0] != get_bound (LEFT))
337 offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS)
338 - relative_coordinate (common[Y_AXIS], Y_AXIS);
346 Slur::get_rods () const
351 r.item_l_drul_[LEFT] = get_bound (LEFT);
352 r.item_l_drul_[RIGHT] = get_bound (RIGHT);
353 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
361 Ugh should have dash-length + dash-period
363 GLUE_SCORE_ELEMENT(Slur,brew_molecule);
365 Slur::member_brew_molecule () const
367 Real thick = paper_l ()->get_var ("slur_thickness");
368 Bezier one = get_curve ();
371 SCM d = get_elt_property ("dashed");
373 a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
375 a = lookup_l ()->slur (one, Directional_element_interface (this).get () * thick, thick);
377 return a.create_scheme();
381 Slur::set_control_points ()
383 Slur_bezier_bow bb (get_encompass_offset_arr (),
384 Directional_element_interface (this).get ());
386 Real staff_space = Staff_symbol_referencer_interface (this).staff_space ();
387 Real h_inf = paper_l ()->get_var ("slur_height_limit_factor") * staff_space;
388 Real r_0 = paper_l ()->get_var ("slur_ratio");
390 bb.set_default_bezier (h_inf, r_0);
392 if (bb.fit_factor () > 1.0)
394 Real length = bb.curve_.control_[3][X_AXIS];
395 Real default_height = bb.get_default_height (h_inf, r_0, length);
396 bb.minimise_enclosed_area (paper_l(), default_height);
398 Real bff = paper_l ()->get_var ("slur_force_blowfit");
399 bb.curve_.control_[1][Y_AXIS] *= bff;
400 bb.curve_.control_[2][Y_AXIS] *= bff;
403 Real sb = paper_l ()->get_var ("slur_beautiful");
404 Real beautiful = length * default_height * sb;
405 Real area = bb.enclosed_area_f ();
408 Slurs that fit beautifully are not ugly
410 if (area > beautiful)
411 de_uglyfy (&bb, default_height);
414 Bezier b = bb.get_bezier ();
417 SCM controls = SCM_EOL;
419 controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
421 set_elt_property ("control-points", controls);
426 Slur::get_curve () const
431 if (!Directional_element_interface (this).get ()
432 || ! gh_symbol_p (index_cell (get_elt_property ("attachment"), LEFT)))
433 ((Slur*)this)->set_extremities ();
435 if (!gh_pair_p (get_elt_property ("control-points")))
436 ((Slur*)this)->set_control_points ();
439 for (SCM s= get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
441 b.control_[i] = ly_scm2offset (gh_car (s));
445 Array<Offset> enc (get_encompass_offset_arr ());
446 Direction dir = Directional_element_interface (this).get ();
448 Real x1 = enc[0][X_AXIS];
449 Real x2 = enc.top ()[X_AXIS];
452 for (int i=1; i < enc.size ()-1; i++)
454 Real x = enc[i][X_AXIS];
457 Real y = b.get_other_coordinate (X_AXIS, x);
458 off = off >? dir * (enc[i][Y_AXIS] - y);
461 b.translate (Offset (0, dir * off));