2 slur.cc -- implement Slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--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"
28 #include "bezier-bow.hh"
30 #include "cross-staff.hh"
31 #include "group-interface.hh"
35 dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = 0.0;
36 dx_f_drul_[LEFT] = dx_f_drul_[RIGHT] = 0.0;
37 set_elt_property ("note-columns", SCM_EOL);
41 Slur::add_column (Note_column*n)
43 if (!gh_pair_p (n->get_elt_property ("note-heads")))
44 warning (_ ("Putting slur over rest. Ignoring."));
47 Group_interface gi (this, "note-columns");
54 Slur::get_default_dir () const
56 Link_array<Note_column> encompass_arr =
57 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
60 for (int i=0; i < encompass_arr.size (); i ++)
62 if (encompass_arr[i]->dir () < 0)
72 Slur::do_add_processing ()
74 Link_array<Note_column> encompass_arr =
75 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
77 if (encompass_arr.size ())
79 set_bounds (LEFT, encompass_arr[0]);
80 if (encompass_arr.size () > 1)
81 set_bounds (RIGHT, encompass_arr.top ());
88 Slur::encompass_offset (Note_column const* col) const
91 Stem* stem_l = col->stem_l ();
92 Direction dir = directional_element (this).get ();
96 warning (_ ("Slur over rest?"));
97 o[X_AXIS] = col->hpos_f ();
98 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
101 Direction stem_dir = directional_element (stem_l).get ();
102 o[X_AXIS] = stem_l->hpos_f ();
105 Simply set x to middle of notehead
108 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
110 if ((stem_dir == dir)
111 && !stem_l->extent (Y_AXIS).empty_b ())
113 o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
117 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
121 leave a gap: slur mustn't touch head/stem
123 o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
124 o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
134 Slur::do_post_processing ()
136 Link_array<Note_column> encompass_arr =
137 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
139 if (!encompass_arr.size ())
141 set_elt_property ("transparent", SCM_BOOL_T);
147 if (!directional_element (this).get ())
148 directional_element (this).set (get_default_dir ());
152 Slur and tie placement [OSU]
155 * x = centre of head - d * x_gap_f
158 * y = length < 5ss : horizontal tangent + d * 0.25 ss
159 y = length >= 5ss : y next interline - d * 0.25 ss
162 Real staff_space = paper_l ()->get_var ("interline");
163 Real half_staff_space = staff_space / 2;
165 Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
166 Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
168 Drul_array<Note_column*> note_column_drul;
169 note_column_drul[LEFT] = encompass_arr[0];
170 note_column_drul[RIGHT] = encompass_arr.top ();
172 bool fix_broken_b = false;
174 Direction my_dir = directional_element (this).get ();
182 if ((note_column_drul[d] == spanned_drul_[d])
183 && note_column_drul[d]->first_head ()
184 && (note_column_drul[d]->stem_l ()))
186 Stem* stem_l = note_column_drul[d]->stem_l ();
188 side directly attached to note head;
189 no beam getting in the way
191 if ((stem_l->extent (Y_AXIS).empty_b ()
192 || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
193 && !((my_dir == stem_l->get_direction ())
194 && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
196 dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
197 dx_f_drul_[d] -= d * x_gap_f;
199 if (stem_l->get_direction () != my_dir)
201 dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
205 dy_f_drul_[d] = stem_l->chord_start_f ()
206 + my_dir * half_staff_space;
208 dy_f_drul_[d] += my_dir * y_gap_f;
211 side attached to (visible) stem
215 dx_f_drul_[d] = stem_l->hpos_f ()
216 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
218 side attached to beamed stem
220 if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
222 dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
223 dy_f_drul_[d] += my_dir * 2 * y_gap_f;
226 side attached to notehead, with stem getting in the way
230 dx_f_drul_[d] -= d * x_gap_f;
232 dy_f_drul_[d] = stem_l->chord_start_f ()
233 + my_dir * half_staff_space;
234 dy_f_drul_[d] += my_dir * y_gap_f;
243 dx_f_drul_[d] = get_broken_left_end_align ();
246 broken: should get y from other piece, so that slur
247 continues up/down trend
249 for now: be horizontal..
254 while (flip (&d) != LEFT);
256 int cross_count = cross_staff_count ();
257 bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
259 Drul_array<Offset> info_drul;
260 Drul_array<Real> interstaff_interval;
264 info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
265 interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
268 while (flip (&d) != LEFT);
270 Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
274 Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
276 dy_f_drul_[d] = info_drul[d][Y_AXIS];
279 dy_f_drul_[d] -= interstaff_interval[d];
280 if (cross_count) // interstaff_i ?
282 dy_f_drul_[LEFT] += interstaff_interval[d];
283 dy_f_drul_[RIGHT] += interstaff_interval[d];
289 dy_f_drul_[RIGHT] += interstaff_f;
296 Now we've got a fine slur
297 Catch and correct some ugly cases
299 String infix = interstaff_b ? "interstaff_" : "";
300 Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
301 Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
302 Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
303 Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
305 Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
306 Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
308 dy_f -= interstaff_f;
311 Avoid too steep slurs.
313 Real slope_ratio_f = abs (dy_f / dx_f);
314 if (slope_ratio_f > slope_damp_f)
316 Direction d = (Direction)(- my_dir * (sign (dy_f)));
319 Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
321 must never change sign of dy
323 damp_f = damp_f <? abs (dy_f);
324 dy_f_drul_[d] += my_dir * damp_f;
330 Wierd slurs may look a lot better after they have been
332 So, we'll do this in 3 steps
334 for (int i = 0; i < 3; i++)
336 Bezier c (get_curve ());
338 Offset size (c.extent (X_AXIS).length (),
339 c.extent (Y_AXIS).length ());
341 dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
343 dy_f -= interstaff_f;
345 Real height_ratio_f = abs (size[Y_AXIS] / size[X_AXIS]);
346 if (height_ratio_f > height_damp_f)
348 Direction d = (Direction)(- my_dir * (sign (dy_f)));
351 /* take third step */
352 Real damp_f = (height_ratio_f - height_damp_f) * size[X_AXIS] / 3;
354 if y positions at about the same height, correct both ends
356 if (abs (dy_f / dx_f ) < slope_damp_f)
358 dy_f_drul_[-d] += my_dir * damp_f;
359 dy_f_drul_[d] += my_dir * damp_f;
362 don't change slope too much, would have been catched by slope damping
366 damp_f = damp_f <? abs (dy_f/2);
367 dy_f_drul_[d] += my_dir * damp_f;
373 If, after correcting, we're close to stem-end...
375 Drul_array<Real> snapy_f_drul;
376 snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
377 Drul_array<Real> snapx_f_drul;
378 snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
379 Drul_array<bool> snapped_b_drul;
380 snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
383 Note_column * nc = note_column_drul[d];
384 if (nc == spanned_drul_[d]
386 && nc->stem_l ()->get_direction () == my_dir
387 && abs (nc->stem_l ()->extent (Y_AXIS)[my_dir]
388 - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
392 prepare to attach to stem-end
394 snapx_f_drul[d] = nc->stem_l ()->hpos_f ()
395 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
397 snapy_f_drul[d] = nc->stem_l ()->extent (Y_AXIS)[my_dir]
398 + interstaff_interval[d]
399 + my_dir * 2 * y_gap_f;
401 snapped_b_drul[d] = true;
404 while (flip (&d) != LEFT);
407 only use snapped positions if sign (dy) will not change
408 and dy doesn't change too much
411 dy_f += interstaff_f;
417 More refactoring could be done.
419 Real maxsnap = abs (dy_f * snap_max_dy_f);
420 if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
421 && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
422 && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
425 dy_f_drul_ = snapy_f_drul;
426 dx_f_drul_ = snapx_f_drul;
431 Direction od = (Direction)-d;
432 if (snapped_b_drul[d]
433 && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
434 && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od] - d * dy_f)
437 dy_f_drul_[d] = snapy_f_drul[d];
438 dx_f_drul_[d] = snapx_f_drul[d];
441 while (flip (&d) != LEFT);
446 Slur::cross_staff_count ()const
448 Link_array<Note_column> encompass_arr =
449 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
453 for (int i = 0; i < encompass_arr.size (); i++)
455 if (calc_interstaff_dist (encompass_arr[i], this))
463 Slur::get_encompass_offset_arr () const
465 Link_array<Note_column> encompass_arr =
466 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
468 Array<Offset> offset_arr;
471 check non-disturbed slur
472 FIXME: x of ends off by a tiny bit!!
474 offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
475 offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
479 Offset origin (relative_coordinate (0, X_AXIS), 0);
482 int last = encompass_arr.size () - 2;
484 offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
490 int cross_count = cross_staff_count ();
491 bool cross_b = cross_count && cross_count < encompass_arr.size ();
492 if (encompass_arr[0] != spanned_drul_[LEFT])
495 Real is = calc_interstaff_dist (encompass_arr[0], this);
497 offset_arr[0][Y_AXIS] += is;
503 if (encompass_arr.top () != spanned_drul_[RIGHT])
508 for (int i = first; i <= last; i++)
510 Offset o (encompass_offset (encompass_arr[i]));
511 offset_arr.push (o - origin);
514 offset_arr.push (Offset (spanner_length ()+ dx_f_drul_[RIGHT],
522 Slur::get_rods () const
526 r.item_l_drul_ = spanned_drul_;
527 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
539 b.translate (-b.control_[0]);
540 Real alpha = b.control_[3].arg ();
543 if (b.control_[1][Y_AXIS] < 0)
545 b.control_[1][Y_AXIS] *= -1;
546 b.control_[2][Y_AXIS] *= -1;
549 Real len = b.control_[3][X_AXIS];
550 Real indent = 10 *b.control_[1][X_AXIS] / len ;
551 Real ht = 10 *b.control_[1][Y_AXIS] / len ;
553 SCM res = scm_eval (scm_listify (ly_symbol2scm ("slur-ugly"), gh_double2scm (indent), gh_double2scm (ht), SCM_UNDEFINED ));
561 Ugh should have dash-length + dash-period
564 Slur::do_brew_molecule_p () const
566 Real thick = paper_l ()->get_var ("slur_thickness");
567 Bezier one = get_curve ();
570 SCM d = get_elt_property ("dashed");
572 a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
574 a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
577 SCM u = ugly_scm (one);
580 Molecule mark = lookup_l ()-> text ( "roman",
581 to_str (gh_scm2double (gh_car (u)), "%0.2f") + "," +
582 to_str(gh_scm2double (gh_cdr (u)), "%0.2f"),
585 mark.translate_axis (20 , Y_AXIS);
586 a.add_molecule (mark);
589 return new Molecule (a);
595 Slur::get_curve () const
597 Array<Offset> enc (get_encompass_offset_arr ());
598 Direction dir = directional_element (this).get ();
599 Bezier_bow b (enc,dir);
601 b.ratio_ = paper_l ()->get_var ("slur_ratio");
602 b.height_limit_ = paper_l ()->get_var ("slur_height_limit");
603 b.rc_factor_ = paper_l ()->get_var ("slur_rc_factor");
607 Bezier curve = b.get_curve ();
609 Real x1 = enc[0][X_AXIS];
610 Real x2 = enc.top ()[X_AXIS];
613 for (int i=1; i < enc.size ()-1; i++)
615 Real x = enc[i][X_AXIS];
618 Real y = curve.get_other_coordinate (X_AXIS, x);
619 off = off >? dir * (enc[i][Y_AXIS] - y);
622 curve.translate (Offset (0, dir * off));