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");
76 set_bounds (LEFT, encompass_arr[0]);
77 if (encompass_arr.size () > 1)
78 set_bounds (RIGHT, encompass_arr.top ());
84 Slur::encompass_offset (Note_column const* col) const
87 Stem* stem_l = col->stem_l ();
88 Direction dir = directional_element (this).get ();
92 warning (_ ("Slur over rest?"));
93 o[X_AXIS] = col->hpos_f ();
94 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
97 Direction stem_dir = directional_element (stem_l).get ();
98 o[X_AXIS] = stem_l->hpos_f ();
101 Simply set x to middle of notehead
104 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
106 if ((stem_dir == dir)
107 && !stem_l->extent (Y_AXIS).empty_b ())
109 o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
113 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
117 leave a gap: slur mustn't touch head/stem
119 o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
120 o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
130 Slur::do_post_processing ()
132 Link_array<Note_column> encompass_arr =
133 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
135 if (!encompass_arr.size ())
137 set_elt_property ("transparent", SCM_BOOL_T);
143 if (!directional_element (this).get ())
144 directional_element (this).set (get_default_dir ());
148 Slur and tie placement [OSU]
151 * x = centre of head - d * x_gap_f
154 * y = length < 5ss : horizontal tangent + d * 0.25 ss
155 y = length >= 5ss : y next interline - d * 0.25 ss
158 Real staff_space = paper_l ()->get_var ("interline");
159 Real half_staff_space = staff_space / 2;
161 Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
162 Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
164 Drul_array<Note_column*> note_column_drul;
165 note_column_drul[LEFT] = encompass_arr[0];
166 note_column_drul[RIGHT] = encompass_arr.top ();
168 bool fix_broken_b = false;
170 Direction my_dir = directional_element (this).get ();
178 if ((note_column_drul[d] == spanned_drul_[d])
179 && note_column_drul[d]->first_head ()
180 && (note_column_drul[d]->stem_l ()))
182 Stem* stem_l = note_column_drul[d]->stem_l ();
184 side directly attached to note head;
185 no beam getting in the way
187 if ((stem_l->extent (Y_AXIS).empty_b ()
188 || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
189 && !((my_dir == stem_l->get_direction ())
190 && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
192 dx_f_drul_[d] = spanned_drul_[d]->extent (X_AXIS).length () / 2;
193 dx_f_drul_[d] -= d * x_gap_f;
195 if (stem_l->get_direction () != my_dir)
197 dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
201 dy_f_drul_[d] = stem_l->chord_start_f ()
202 + my_dir * half_staff_space;
204 dy_f_drul_[d] += my_dir * y_gap_f;
207 side attached to (visible) stem
211 dx_f_drul_[d] = stem_l->hpos_f ()
212 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
214 side attached to beamed stem
216 if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
218 dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
219 dy_f_drul_[d] += my_dir * 2 * y_gap_f;
222 side attached to notehead, with stem getting in the way
226 dx_f_drul_[d] -= d * x_gap_f;
228 dy_f_drul_[d] = stem_l->chord_start_f ()
229 + my_dir * half_staff_space;
230 dy_f_drul_[d] += my_dir * y_gap_f;
239 dx_f_drul_[d] = get_broken_left_end_align ();
242 broken: should get y from other piece, so that slur
243 continues up/down trend
245 for now: be horizontal..
250 while (flip (&d) != LEFT);
252 int cross_count = cross_staff_count ();
253 bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
255 Drul_array<Offset> info_drul;
256 Drul_array<Real> interstaff_interval;
260 info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
261 interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
264 while (flip (&d) != LEFT);
266 Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
270 Direction d = (encompass_arr.top () != spanned_drul_[RIGHT]) ?
272 dy_f_drul_[d] = info_drul[d][Y_AXIS];
275 dy_f_drul_[d] -= interstaff_interval[d];
276 if (cross_count) // interstaff_i ?
278 dy_f_drul_[LEFT] += interstaff_interval[d];
279 dy_f_drul_[RIGHT] += interstaff_interval[d];
285 dy_f_drul_[RIGHT] += interstaff_f;
292 Now we've got a fine slur
293 Catch and correct some ugly cases
295 String infix = interstaff_b ? "interstaff_" : "";
296 Real height_damp_f = paper_l ()->get_var ("slur_"+infix +"height_damping");
297 Real slope_damp_f = paper_l ()->get_var ("slur_"+infix +"slope_damping");
298 Real snap_f = paper_l ()->get_var ("slur_"+infix +"snap_to_stem");
299 Real snap_max_dy_f = paper_l ()->get_var ("slur_"+infix +"snap_max_slope_change");
301 Real dx_f = spanner_length ()+ dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
302 Real dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
304 dy_f -= interstaff_f;
307 Avoid too steep slurs.
309 Real slope_ratio_f = abs (dy_f / dx_f);
310 if (slope_ratio_f > slope_damp_f)
312 Direction d = (Direction)(- my_dir * (sign (dy_f)));
315 Real damp_f = (slope_ratio_f - slope_damp_f) * dx_f;
317 must never change sign of dy
319 damp_f = damp_f <? abs (dy_f);
320 dy_f_drul_[d] += my_dir * damp_f;
326 Wierd slurs may look a lot better after they have been
328 So, we'll do this in 3 steps
330 for (int i = 0; i < 3; i++)
332 Bezier c (get_curve ());
334 Offset size (c.extent (X_AXIS).length (),
335 c.extent (Y_AXIS).length ());
337 dy_f = dy_f_drul_[RIGHT] - dy_f_drul_[LEFT];
339 dy_f -= interstaff_f;
341 Real height_ratio_f = abs (size[Y_AXIS] / size[X_AXIS]);
342 if (height_ratio_f > height_damp_f)
344 Direction d = (Direction)(- my_dir * (sign (dy_f)));
347 /* take third step */
348 Real damp_f = (height_ratio_f - height_damp_f) * size[X_AXIS] / 3;
350 if y positions at about the same height, correct both ends
352 if (abs (dy_f / dx_f ) < slope_damp_f)
354 dy_f_drul_[-d] += my_dir * damp_f;
355 dy_f_drul_[d] += my_dir * damp_f;
358 don't change slope too much, would have been catched by slope damping
362 damp_f = damp_f <? abs (dy_f/2);
363 dy_f_drul_[d] += my_dir * damp_f;
369 If, after correcting, we're close to stem-end...
371 Drul_array<Real> snapy_f_drul;
372 snapy_f_drul[LEFT] = snapy_f_drul[RIGHT] = 0;
373 Drul_array<Real> snapx_f_drul;
374 snapx_f_drul[LEFT] = snapx_f_drul[RIGHT] = 0;
375 Drul_array<bool> snapped_b_drul;
376 snapped_b_drul[LEFT] = snapped_b_drul[RIGHT] = false;
379 Note_column * nc = note_column_drul[d];
380 if (nc == spanned_drul_[d]
382 && nc->stem_l ()->get_direction () == my_dir
383 && abs (nc->stem_l ()->extent (Y_AXIS)[my_dir]
384 - dy_f_drul_[d] + (d == LEFT ? 0 : interstaff_f))
388 prepare to attach to stem-end
390 snapx_f_drul[d] = nc->stem_l ()->hpos_f ()
391 - spanned_drul_[d]->relative_coordinate (0, X_AXIS);
393 snapy_f_drul[d] = nc->stem_l ()->extent (Y_AXIS)[my_dir]
394 + interstaff_interval[d]
395 + my_dir * 2 * y_gap_f;
397 snapped_b_drul[d] = true;
400 while (flip (&d) != LEFT);
403 only use snapped positions if sign (dy) will not change
404 and dy doesn't change too much
407 dy_f += interstaff_f;
413 More refactoring could be done.
415 Real maxsnap = abs (dy_f * snap_max_dy_f);
416 if (snapped_b_drul[LEFT] && snapped_b_drul[RIGHT]
417 && ((sign (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT]) == sign (dy_f)))
418 && (!dy_f || (abs (snapy_f_drul[RIGHT] - snapy_f_drul[LEFT] - dy_f)
421 dy_f_drul_ = snapy_f_drul;
422 dx_f_drul_ = snapx_f_drul;
427 Direction od = (Direction)-d;
428 if (snapped_b_drul[d]
429 && d * sign (snapy_f_drul[d] - dy_f_drul_[od]) == sign (dy_f)
430 && (!dy_f || (abs (snapy_f_drul[d] - dy_f_drul_[od] - d * dy_f)
433 dy_f_drul_[d] = snapy_f_drul[d];
434 dx_f_drul_[d] = snapx_f_drul[d];
437 while (flip (&d) != LEFT);
442 Slur::cross_staff_count ()const
444 Link_array<Note_column> encompass_arr =
445 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
449 for (int i = 0; i < encompass_arr.size (); i++)
451 if (calc_interstaff_dist (encompass_arr[i], this))
459 Slur::get_encompass_offset_arr () const
461 Link_array<Note_column> encompass_arr =
462 Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
464 Array<Offset> offset_arr;
467 check non-disturbed slur
468 FIXME: x of ends off by a tiny bit!!
470 offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
471 offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
475 Offset origin (relative_coordinate (0, X_AXIS), 0);
478 int last = encompass_arr.size () - 2;
480 offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
486 int cross_count = cross_staff_count ();
487 bool cross_b = cross_count && cross_count < encompass_arr.size ();
488 if (encompass_arr[0] != spanned_drul_[LEFT])
491 Real is = calc_interstaff_dist (encompass_arr[0], this);
493 offset_arr[0][Y_AXIS] += is;
499 if (encompass_arr.top () != spanned_drul_[RIGHT])
504 for (int i = first; i <= last; i++)
506 Offset o (encompass_offset (encompass_arr[i]));
507 offset_arr.push (o - origin);
510 offset_arr.push (Offset (spanner_length ()+ dx_f_drul_[RIGHT],
518 Slur::get_rods () const
522 r.item_l_drul_ = spanned_drul_;
523 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
535 b.translate (-b.control_[0]);
536 Real alpha = b.control_[3].arg ();
539 if (b.control_[1][Y_AXIS] < 0)
541 b.control_[1][Y_AXIS] *= -1;
542 b.control_[2][Y_AXIS] *= -1;
545 Real len = b.control_[3][X_AXIS];
546 Real indent = 10 *b.control_[1][X_AXIS] / len ;
547 Real ht = 10 *b.control_[1][Y_AXIS] / len ;
549 SCM res = scm_eval (scm_listify (ly_symbol2scm ("slur-ugly"), gh_double2scm (indent), gh_double2scm (ht), SCM_UNDEFINED ));
557 Ugh should have dash-length + dash-period
560 Slur::do_brew_molecule_p () const
562 Real thick = paper_l ()->get_var ("slur_thickness");
563 Bezier one = get_curve ();
566 SCM d = get_elt_property ("dashed");
568 a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
570 a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
573 SCM u = ugly_scm (one);
576 Molecule mark = lookup_l ()-> text ( "roman",
577 to_str (gh_scm2double (gh_car (u)), "%0.2f") + "," +
578 to_str(gh_scm2double (gh_cdr (u)), "%0.2f"),
581 mark.translate_axis (20 , Y_AXIS);
582 a.add_molecule (mark);
585 return new Molecule (a);
591 Slur::get_curve () const
593 Array<Offset> enc (get_encompass_offset_arr ());
594 Direction dir = directional_element (this).get ();
595 Bezier_bow b (enc,dir);
597 b.ratio_ = paper_l ()->get_var ("slur_ratio");
598 b.height_limit_ = paper_l ()->get_var ("slur_height_limit");
599 b.rc_factor_ = paper_l ()->get_var ("slur_rc_factor");
603 Bezier curve = b.get_curve ();
605 Real x1 = enc[0][X_AXIS];
606 Real x2 = enc.top ()[X_AXIS];
609 for (int i=1; i < enc.size ()-1; i++)
611 Real x = enc[i][X_AXIS];
614 Real y = curve.get_other_coordinate (X_AXIS, x);
615 off = off >? dir * (enc[i][Y_AXIS] - y);
618 curve.translate (Offset (0, dir * off));