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
19 #include "paper-def.hh"
20 #include "note-column.hh"
22 #include "paper-column.hh"
23 #include "molecule.hh"
28 #include "cross-staff.hh"
35 Slur::add_column (Note_column*n)
37 if (!n->head_l_arr_.size ())
38 warning (_ ("Putting slur over rest. Ignoring."));
41 encompass_arr_.push (n);
47 Slur::get_default_dir () const
50 for (int i=0; i < encompass_arr_.size (); i ++)
52 if (encompass_arr_[i]->dir () < 0)
62 Slur::do_add_processing ()
64 set_bounds (LEFT, encompass_arr_[0]);
65 if (encompass_arr_.size () > 1)
66 set_bounds (RIGHT, encompass_arr_.top ());
70 Slur::do_pre_processing ()
72 // don't set directions
76 Slur::do_substitute_element_pointer (Score_element*o, Score_element*n)
79 while ((i = encompass_arr_.find_i (dynamic_cast<Note_column *> (o))) >=0)
82 encompass_arr_[i] = dynamic_cast<Note_column *> (n);
84 encompass_arr_.del (i);
89 Note_column_compare (Note_column *const&n1 , Note_column* const&n2)
91 return Item::left_right_compare (n1, n2);
96 Slur::encompass_offset (Note_column const* col) const
99 Stem* stem_l = col->stem_l_;
102 warning (_ ("Slur over rest?"));
103 o[X_AXIS] = col->hpos_f ();
104 o[Y_AXIS] = col->extent (Y_AXIS)[dir_];
108 o[X_AXIS] = stem_l->hpos_f ();
111 Simply set x to middle of notehead
114 o[X_AXIS] -= 0.5 * stem_l->dir_ * col->extent (X_AXIS).length ();
116 if ((stem_l->dir_ == dir_)
117 && !stem_l->extent (Y_AXIS).empty_b ())
119 o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir_];
123 o[Y_AXIS] = col->extent (Y_AXIS)[dir_];
127 leave a gap: slur mustn't touch head/stem
129 o[Y_AXIS] += dir_ * paper_l ()->get_var ("slur_y_free");
130 o[Y_AXIS] += calc_interstaff_dist (stem_l, this);
140 Slur::do_post_processing ()
142 encompass_arr_.sort (Note_column_compare);
144 dir_ = get_default_dir ();
147 Slur and tie placement [OSU]
150 * x = centre of head - d * x_gap_f
153 * y = length < 5ss : horizontal tangent + d * 0.25 ss
154 y = length >= 5ss : y next interline - d * 0.25 ss
157 Real interline_f = paper_l ()->get_realvar (interline_scm_sym);
158 Real internote_f = interline_f / 2;
160 Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
161 Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
163 Drul_array<Note_column*> note_column_drul;
164 note_column_drul[LEFT] = encompass_arr_[0];
165 note_column_drul[RIGHT] = encompass_arr_.top ();
167 bool fix_broken_b = false;
171 dx_f_drul_[d] = dy_f_drul_[d] = 0;
172 if ((note_column_drul[d] == spanned_drul_[d])
173 && note_column_drul[d]->head_l_arr_.size ()
174 && (note_column_drul[d]->stem_l_))
176 Stem* stem_l = note_column_drul[d]->stem_l_;
178 side directly attached to note head;
179 no beam getting in the way
181 if ((stem_l->extent (Y_AXIS).empty_b ()
182 || !((stem_l->dir_ == dir_) && (dir_ != d)))
183 && !((dir_ == stem_l->dir_)
184 && stem_l->beam_l_ && (stem_l->beams_i_drul_[-d] >= 1)))
186 dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
187 dx_f_drul_[d] -= d * x_gap_f;
189 if (stem_l->dir_ != dir_)
191 dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[dir_];
195 dy_f_drul_[d] = stem_l->chord_start_f ()
196 + dir_ * internote_f;
198 dy_f_drul_[d] += dir_ * y_gap_f;
201 side attached to (visible) stem
205 dx_f_drul_[d] = stem_l->hpos_f ()
206 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
208 side attached to beamed stem
210 if (stem_l->beam_l_ && (stem_l->beams_i_drul_[-d] >= 1))
212 dy_f_drul_[d] = stem_l->extent (Y_AXIS)[dir_];
213 dy_f_drul_[d] += dir_ * 2 * y_gap_f;
216 side attached to notehead, with stem getting in the way
220 dx_f_drul_[d] -= d * x_gap_f;
222 dy_f_drul_[d] = stem_l->chord_start_f ()
223 + dir_ * internote_f;
224 dy_f_drul_[d] += dir_ * y_gap_f;
233 dx_f_drul_[d] = get_broken_left_end_align ();
236 broken: should get y from other piece, so that slur
237 continues up/down trend
239 for now: be horizontal..
244 while (flip (&d) != LEFT);
246 int cross_count = cross_staff_count ();
247 bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr_.size ());
249 Drul_array<Offset> info_drul;
250 Drul_array<Real> interstaff_interval;
254 info_drul[d] = encompass_offset (encompass_arr_.boundary (d, 0));
255 interstaff_interval[d] = calc_interstaff_dist (encompass_arr_.boundary (d,0),
258 while (flip (&d) != LEFT);
260 Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
264 Direction d = (encompass_arr_.top () != spanned_drul_[RIGHT]) ?
266 dy_f_drul_[d] = info_drul[d][Y_AXIS];
269 dy_f_drul_[d] -= interstaff_interval[d];
270 if (cross_count) // interstaff_i ?
272 dy_f_drul_[LEFT] += interstaff_interval[d];
273 dy_f_drul_[RIGHT] += interstaff_interval[d];
280 Now we've got a fine slur
281 Catch and correct some ugly cases
286 String infix = interstaff_b ? "interstaff_" : "";
287 Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
288 Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
289 Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
290 Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
293 dy_f_drul_[RIGHT] += interstaff_f;
295 Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
297 dy_f -= interstaff_f;
298 Real dx_f = do_width ().length () + dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
301 Avoid too steep slurs.
303 Real slope_ratio_f = abs (dy_f / dx_f);
304 if (slope_ratio_f > slope_damp_f)
306 Direction d = (Direction)(- dir_ * (sign (dy_f)));
309 Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
311 must never change sign of dy
313 damp_f = damp_f <? abs (dy_f);
314 dy_f_drul_[d] += dir_ * damp_f;
320 Wierd slurs may look a lot better after they have been
322 So, we'll do this in 3 steps
324 for (int i = 0; i < 3; i++)
326 Drul_array<Interval> curve_xy_drul = curve_extent_drul ();
327 Real height_f = curve_xy_drul[Y].length ();
328 Real width_f = curve_xy_drul[X].length ();
330 dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
332 dy_f -= interstaff_f;
334 Real height_ratio_f = abs (height_f / width_f);
335 if (height_ratio_f > height_damp_f)
337 Direction d = (Direction)(- dir_ * (sign (dy_f)));
340 /* take third step */
341 Real damp_f = (height_ratio_f - height_damp_f) * width_f / 3;
343 if y positions at about the same height, correct both ends
345 if (abs (dy_f / dx_f ) < slope_damp_f)
347 dy_f_drul_[-d] += dir_ * damp_f;
348 dy_f_drul_[d] += dir_ * damp_f;
351 don't change slope too much, would have been catched by slope damping
355 damp_f = damp_f <? abs (dy_f/2);
356 dy_f_drul_[d] += dir_ * damp_f;
362 If, after correcting, we're close to stem-end...
364 Drul_array<Real> snapy_f_drul;
365 snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
366 Drul_array<Real> snapx_f_drul;
367 snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
368 Drul_array<bool> snapped_b_drul;
369 snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
372 Note_column * nc = note_column_drul[d];
373 if (nc == spanned_drul_[d]
375 && nc->stem_l_->dir_ == dir_
376 && abs (nc->stem_l_->extent (Y_AXIS)[dir_]
377 - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
381 prepare to attach to stem-end
383 snapx_f_drul[d] = nc->stem_l_->hpos_f ()
384 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
386 snapy_f_drul[d] = nc->stem_l_->extent (Y_AXIS)[dir_]
387 + interstaff_interval[d]
388 + dir_ * 2 * y_gap_f;
390 snapped_b_drul[d] = true;
393 while (flip (&d) != LEFT);
396 only use snapped positions if sign (dy) will not change
397 and dy doesn't change too much
400 dy_f += interstaff_f;
406 More refactoring could be done.
408 Real maxsnap = abs (dy_f * snap_max_dy_f);
409 if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
410 && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
411 && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
414 dy_f_drul_ = snapy_f_drul;
415 dx_f_drul_ = snapx_f_drul;
420 Direction od = (Direction)-d;
421 if (snapped_b_drul[d]
422 && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
423 && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od] - d * dy_f)
426 dy_f_drul_[d] = snapy_f_drul[d];
427 dx_f_drul_[d] = snapx_f_drul[d];
430 while (flip (&d) != LEFT);
435 Slur::cross_staff_count ()const
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 Array<Offset> offset_arr;
454 check non-disturbed slur
455 FIXME: x of ends off by a tiny bit!!
457 offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
458 offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
462 Offset origin (relative_coordinate (0, X_AXIS), 0);
465 int last = encompass_arr_.size () - 2;
467 offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
473 int cross_count = cross_staff_count ();
474 bool cross_b = cross_count && cross_count < encompass_arr_.size ();
475 if (encompass_arr_[0] != spanned_drul_[LEFT])
478 Real is = calc_interstaff_dist (encompass_arr_[0], this);
480 offset_arr[0][Y_AXIS] += is;
486 if (encompass_arr_.top () != spanned_drul_[RIGHT])
491 for (int i = first; i <= last; i++)
493 Offset o (encompass_offset (encompass_arr_[i]));
494 offset_arr.push (o - origin);
497 offset_arr.push (Offset (do_width ().length () + dx_f_drul_[RIGHT],
505 Slur::get_rods () const
509 r.item_l_drul_ = spanned_drul_;
510 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");