2 slur.cc -- implement Slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--1999 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 "group-interface.hh"
19 #include "paper-def.hh"
20 #include "note-column.hh"
22 #include "paper-column.hh"
23 #include "molecule.hh"
28 #include "cross-staff.hh"
29 #include "group-interface.hh"
33 set_elt_property ("note-columns", SCM_EOL);
37 Slur::add_column (Note_column*n)
39 if (!gh_pair_p (n->get_elt_property ("note-heads")))
40 warning (_ ("Putting slur over rest. Ignoring."));
43 Group_interface gi (this, "note-columns");
50 Slur::get_default_dir () const
52 Link_array<Note_column> encompass_arr =
53 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
56 for (int i=0; i < encompass_arr.size (); i ++)
58 if (encompass_arr[i]->dir () < 0)
68 Slur::do_add_processing ()
70 Link_array<Note_column> encompass_arr =
71 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
72 set_bounds (LEFT, encompass_arr[0]);
73 if (encompass_arr.size () > 1)
74 set_bounds (RIGHT, encompass_arr.top ());
78 Slur::do_pre_processing ()
80 // don't set directions
85 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
87 return Item::left_right_compare (n1, n2);
92 Slur::encompass_offset (Note_column const* col) const
95 Stem* stem_l = col->stem_l ();
98 warning (_ ("Slur over rest?"));
99 o[X_AXIS] = col->hpos_f ();
100 o[Y_AXIS] = col->extent (Y_AXIS)[get_direction ()];
104 o[X_AXIS] = stem_l->hpos_f ();
107 Simply set x to middle of notehead
110 o[X_AXIS] -= 0.5 * stem_l->get_direction () * col->extent (X_AXIS).length ();
112 if ((stem_l->get_direction () == get_direction ())
113 && !stem_l->extent (Y_AXIS).empty_b ())
115 o[Y_AXIS] = stem_l->extent (Y_AXIS)[get_direction ()];
119 o[Y_AXIS] = col->extent (Y_AXIS)[get_direction ()];
123 leave a gap: slur mustn't touch head/stem
125 o[Y_AXIS] += get_direction () * paper_l ()->get_var ("slur_y_free");
126 o[Y_AXIS] += calc_interstaff_dist (stem_l, this);
136 Slur::do_post_processing ()
138 Link_array<Note_column> encompass_arr =
139 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
141 encompass_arr.sort (Note_column_compare);
142 if (!get_direction ())
143 set_direction (get_default_dir ());
146 Slur and tie placement [OSU]
149 * x = centre of head - d * x_gap_f
152 * y = length < 5ss : horizontal tangent + d * 0.25 ss
153 y = length >= 5ss : y next interline - d * 0.25 ss
156 Real interline_f = paper_l ()->get_var ("interline");
157 Real internote_f = interline_f / 2;
159 Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
160 Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
162 Drul_array<Note_column*> note_column_drul;
163 note_column_drul[LEFT] = encompass_arr[0];
164 note_column_drul[RIGHT] = encompass_arr.top ();
166 bool fix_broken_b = false;
173 if ((note_column_drul[d] == spanned_drul_[d])
174 && note_column_drul[d]->first_head ()
175 && (note_column_drul[d]->stem_l ()))
177 Stem* stem_l = note_column_drul[d]->stem_l ();
179 side directly attached to note head;
180 no beam getting in the way
182 if ((stem_l->extent (Y_AXIS).empty_b ()
183 || !((stem_l->get_direction () == get_direction ()) && (get_direction () != d)))
184 && !((get_direction () == stem_l->get_direction ())
185 && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
187 dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
188 dx_f_drul_[d] -= d * x_gap_f;
190 if (stem_l->get_direction () != get_direction ())
192 dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[get_direction ()];
196 dy_f_drul_[d] = stem_l->chord_start_f ()
197 + get_direction () * internote_f;
199 dy_f_drul_[d] += get_direction () * y_gap_f;
202 side attached to (visible) stem
206 dx_f_drul_[d] = stem_l->hpos_f ()
207 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
209 side attached to beamed stem
211 if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
213 dy_f_drul_[d] = stem_l->extent (Y_AXIS)[get_direction ()];
214 dy_f_drul_[d] += get_direction () * 2 * y_gap_f;
217 side attached to notehead, with stem getting in the way
221 dx_f_drul_[d] -= d * x_gap_f;
223 dy_f_drul_[d] = stem_l->chord_start_f ()
224 + get_direction () * internote_f;
225 dy_f_drul_[d] += get_direction () * y_gap_f;
234 dx_f_drul_[d] = get_broken_left_end_align ();
237 broken: should get y from other piece, so that slur
238 continues up/down trend
240 for now: be horizontal..
245 while (flip (&d) != LEFT);
247 int cross_count = cross_staff_count ();
248 bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
250 Drul_array<Offset> info_drul;
251 Drul_array<Real> interstaff_interval;
255 info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
256 interstaff_interval[d] = calc_interstaff_dist (encompass_arr.boundary (d,0),
259 while (flip (&d) != LEFT);
261 Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
265 Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
267 dy_f_drul_[d] = info_drul[d][Y_AXIS];
270 dy_f_drul_[d] -= interstaff_interval[d];
271 if (cross_count) // interstaff_i ?
273 dy_f_drul_[LEFT] += interstaff_interval[d];
274 dy_f_drul_[RIGHT] += interstaff_interval[d];
281 Now we've got a fine slur
282 Catch and correct some ugly cases
284 String infix = interstaff_b ? "interstaff_" : "";
285 Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
286 Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
287 Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
288 Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
291 dy_f_drul_[RIGHT] += interstaff_f;
293 Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
295 dy_f -= interstaff_f;
296 Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
299 Avoid too steep slurs.
301 Real slope_ratio_f = abs (dy_f / dx_f);
302 if (slope_ratio_f > slope_damp_f)
304 Direction d = (Direction)(- get_direction () * (sign (dy_f)));
307 Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
309 must never change sign of dy
311 damp_f = damp_f <? abs (dy_f);
312 dy_f_drul_[d] += get_direction () * damp_f;
318 Wierd slurs may look a lot better after they have been
320 So, we'll do this in 3 steps
322 for (int i = 0; i < 3; i++)
324 Real height_f = curve_extent (Y_AXIS).length ();
325 Real width_f = curve_extent (X_AXIS).length ();
327 dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
329 dy_f -= interstaff_f;
331 Real height_ratio_f = abs (height_f / width_f);
332 if (height_ratio_f > height_damp_f)
334 Direction d = (Direction)(- get_direction () * (sign (dy_f)));
337 /* take third step */
338 Real damp_f = (height_ratio_f - height_damp_f) * width_f / 3;
340 if y positions at about the same height, correct both ends
342 if (abs (dy_f / dx_f ) < slope_damp_f)
344 dy_f_drul_[-d] += get_direction () * damp_f;
345 dy_f_drul_[d] += get_direction () * damp_f;
348 don't change slope too much, would have been catched by slope damping
352 damp_f = damp_f <? abs (dy_f/2);
353 dy_f_drul_[d] += get_direction () * damp_f;
359 If, after correcting, we're close to stem-end...
361 Drul_array<Real> snapy_f_drul;
362 snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
363 Drul_array<Real> snapx_f_drul;
364 snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
365 Drul_array<bool> snapped_b_drul;
366 snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
369 Note_column * nc = note_column_drul[d];
370 if (nc == spanned_drul_[d]
372 && nc->stem_l ()->get_direction () == get_direction ()
373 && abs (nc->stem_l ()->extent (Y_AXIS)[get_direction ()]
374 - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
378 prepare to attach to stem-end
380 snapx_f_drul[d] = nc->stem_l ()->hpos_f ()
381 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
383 snapy_f_drul[d] = nc->stem_l ()->extent (Y_AXIS)[get_direction ()]
384 + interstaff_interval[d]
385 + get_direction () * 2 * y_gap_f;
387 snapped_b_drul[d] = true;
390 while (flip (&d) != LEFT);
393 only use snapped positions if sign (dy) will not change
394 and dy doesn't change too much
397 dy_f += interstaff_f;
403 More refactoring could be done.
405 Real maxsnap = abs (dy_f * snap_max_dy_f);
406 if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
407 && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
408 && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
411 dy_f_drul_ = snapy_f_drul;
412 dx_f_drul_ = snapx_f_drul;
417 Direction od = (Direction)-d;
418 if (snapped_b_drul[d]
419 && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
420 && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od] - d * dy_f)
423 dy_f_drul_[d] = snapy_f_drul[d];
424 dx_f_drul_[d] = snapx_f_drul[d];
427 while (flip (&d) != LEFT);
432 Slur::cross_staff_count ()const
434 Link_array<Note_column> encompass_arr =
435 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
439 for (int i = 0; i < encompass_arr.size (); i++)
441 if (calc_interstaff_dist (encompass_arr[i], this))
449 Slur::get_encompass_offset_arr () const
451 Link_array<Note_column> encompass_arr =
452 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
454 Array<Offset> offset_arr;
457 check non-disturbed slur
458 FIXME: x of ends off by a tiny bit!!
460 offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
461 offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
465 Offset origin (relative_coordinate (0, X_AXIS), 0);
468 int last = encompass_arr.size () - 2;
470 offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
476 int cross_count = cross_staff_count ();
477 bool cross_b = cross_count && cross_count < encompass_arr.size ();
478 if (encompass_arr[0] != spanned_drul_[LEFT])
481 Real is = calc_interstaff_dist (encompass_arr[0], this);
483 offset_arr[0][Y_AXIS] += is;
489 if (encompass_arr.top () != spanned_drul_[RIGHT])
494 for (int i = first; i <= last; i++)
496 Offset o (encompass_offset (encompass_arr[i]));
497 offset_arr.push (o - origin);
500 offset_arr.push (Offset (spanner_length ()+ dx_f_drul_[RIGHT],
508 Slur::get_rods () const
512 r.item_l_drul_ = spanned_drul_;
513 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");