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 * 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"
27 #include "bezier-bow.hh"
29 #include "cross-staff.hh"
30 #include "group-interface.hh"
31 #include "staff-symbol-referencer.hh"
35 class Slur_bezier_bow : public Bezier_bow
38 Slur_bezier_bow (Array<Offset> encompass, Direction dir);
39 Array<Real> area_x_gradients_array (Real area);
41 Real enclosed_area_f () const;
42 Real fit_factor () const;
43 void minimise_enclosed_area (Paper_def* paper_l, Real default_height);
46 Slur_bezier_bow::Slur_bezier_bow (Array<Offset> encompass, Direction dir)
47 : Bezier_bow (encompass, dir)
52 Slur_bezier_bow::blow_fit ()
54 Real len = curve_.control_[3][X_AXIS];
55 Real h = curve_.control_[1][Y_AXIS] * fit_factor () / len;
56 curve_.control_[1][Y_AXIS] = h * len;
57 curve_.control_[2][Y_AXIS] = h * len;
58 curve_.assert_sanity ();
63 Slur_bezier_bow::enclosed_area_f () const
66 for (int i=0; i < encompass_.size (); i++)
72 x = Interval (0, encompass_[1][X_AXIS] / 2);
74 curve_.get_other_coordinate (X_AXIS,
78 else if (i == encompass_.size () - 1)
80 x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS])/2,
81 encompass_[i][X_AXIS]);
83 (curve_.get_other_coordinate (X_AXIS,
84 (x[MIN] + x[MAX]) / 2)));
88 x = Interval ((encompass_[i-1][X_AXIS] + encompass_[i][X_AXIS]) / 2,
89 (encompass_[i][X_AXIS] + encompass_[i+1][X_AXIS]) / 2);
90 y = Interval (encompass_[i][Y_AXIS],
91 (curve_.get_other_coordinate (X_AXIS, x[MIN])
92 + curve_.get_other_coordinate (X_AXIS,
93 (x[MIN] + x[MAX]) / 2)
94 + curve_.get_other_coordinate (X_AXIS, x[MAX])) / 3);
97 Real da = x.length () * y.length ();
104 Slur_bezier_bow::area_x_gradients_array (Real area)
106 Real len = curve_.control_[3][X_AXIS];
107 Real grow = len / 10.0;
109 for (int i=0; i < 2; i++)
111 Real r = curve_.control_[i+1][X_AXIS];
112 curve_.control_[i+1][X_AXIS] += grow;
113 da[i] = (enclosed_area_f () - area) / grow;
114 curve_.control_[i+1][X_AXIS] = r;
120 Slur_bezier_bow::minimise_enclosed_area (Paper_def* paper_l,
123 Real length = curve_.control_[3][X_AXIS];
124 Real sb = paper_l->get_var ("slur_beautiful");
125 Real beautiful = length * default_height * sb;
127 DEBUG_OUT << to_str ("Beautiful: %f\n", beautiful);
128 DEBUG_OUT << to_str ("Length: %f\n", length);
129 DEBUG_OUT << to_str ("D-height: %f\n", default_height);
130 DEBUG_OUT << to_str ("FitFac: %f\n", fit_factor ());
132 if (fit_factor () > 1.0)
135 Real pct_c0 = paper_l->get_var ("bezier_pct_c0");
136 Real pct_c3 = paper_l->get_var ("bezier_pct_c3");
137 Real pct_in_max = paper_l->get_var ("bezier_pct_in_max");
138 Real pct_out_max = paper_l->get_var ("bezier_pct_out_max");
139 Real steps = paper_l->get_var ("bezier_area_steps");
141 for (int i=0; i < steps; i++)
143 Real area = enclosed_area_f ();
145 DEBUG_OUT << to_str ("Init area: %f\n", area);
147 if (area <= beautiful)
150 Array<Real> da = area_x_gradients_array (area);
153 Real pct = pct_c0 + pct_c3 * length * length * length;
154 pct *= (steps - i) / steps;
155 if (da[0] > 0 || da[1] < 0)
156 pct = pct <? pct_out_max;
158 pct = pct <? pct_in_max;
160 Real u = (abs (curve_.control_[1][X_AXIS] / da[0])
161 <? abs ((curve_.control_[3][X_AXIS]
162 - curve_.control_[2][X_AXIS]) / da[1]));
164 DEBUG_OUT << to_str ("pct: %f\n", pct);
165 DEBUG_OUT << to_str ("u: %f\n", u);
167 DEBUG_OUT << to_str ("da: (%f, %f)\n", da[0], da[1]);
168 DEBUG_OUT << to_str ("da*u: (%f, %f)\n", da[0]*u*pct, da[1]*u*pct);
169 DEBUG_OUT << to_str ("cx: (%f, %f)\n", curve_.control_[1][X_AXIS],
170 curve_.control_[2][X_AXIS]);
172 curve_.control_[1][X_AXIS] -= da[0] * u * pct;
173 curve_.control_[2][X_AXIS] -= da[1] * u * pct;
176 Real area = enclosed_area_f ();
177 DEBUG_OUT << to_str ("Exarea: %f\n", area);
183 max ( encompass.y / curve.y )
187 Slur_bezier_bow::fit_factor () const
189 Real x1 = encompass_[0][X_AXIS];
190 Real x2 = encompass_.top ()[X_AXIS];
193 for (int i=1; i < encompass_.size ()-1; i++)
195 if (encompass_[i][X_AXIS] > x1 && encompass_[i][X_AXIS] < x2)
197 Real y = curve_.get_other_coordinate (X_AXIS, encompass_[i][X_AXIS]);
200 Real f = encompass_[i][Y_AXIS] / y;
201 factor = factor >? f;
222 dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = 0.0;
223 dx_f_drul_[LEFT] = dx_f_drul_[RIGHT] = 0.0;
225 set_elt_pointer ("note-columns", SCM_EOL);
226 set_elt_property ("control-points", SCM_EOL);
230 Slur::add_column (Note_column*n)
232 if (!gh_pair_p (n->get_elt_pointer ("note-heads")))
233 warning (_ ("Putting slur over rest. Ignoring."));
236 Pointer_group_interface (this, "note-columns").add_element (n);
242 Slur::de_uglyfy (Slur_bezier_bow* bb, Real default_height)
244 Real length = bb->curve_.control_[3][X_AXIS] ;
245 Real ff = bb->fit_factor ();
246 for (int i = 1; i < 3; i++)
248 Real ind = abs (bb->curve_.control_[(i-1)*3][X_AXIS]
249 - bb->curve_.control_[i][X_AXIS]) / length;
250 Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
252 Real f = default_height / length;
253 Real c1 = paper_l ()->get_var ("bezier_control1");
254 Real c2 = paper_l ()->get_var ("bezier_control2");
255 Real c3 = paper_l ()->get_var ("bezier_control3");
260 else if (h > c2 + c3 * ind)
265 bb->curve_.control_[i][Y_AXIS] = h * length;
268 bb->curve_.assert_sanity ();
272 Slur::get_default_dir () const
274 Link_array<Note_column> encompass_arr =
275 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
278 for (int i=0; i < encompass_arr.size (); i ++)
280 if (encompass_arr[i]->dir () < 0)
290 Slur::do_add_processing ()
292 Link_array<Note_column> encompass_arr =
293 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
295 if (encompass_arr.size ())
297 set_bound (LEFT, encompass_arr[0]);
298 if (encompass_arr.size () > 1)
299 set_bound (RIGHT, encompass_arr.top ());
306 Slur::encompass_offset (Note_column const* col) const
309 Stem* stem_l = col->stem_l ();
310 Direction dir = directional_element (this).get ();
314 warning (_ ("Slur over rest?"));
315 o[X_AXIS] = col->relative_coordinate (0, X_AXIS);
316 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
319 Direction stem_dir = directional_element (stem_l).get ();
320 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
323 Simply set x to middle of notehead
326 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
328 if ((stem_dir == dir)
329 && !stem_l->extent (Y_AXIS).empty_b ())
331 o[Y_AXIS] = stem_l->extent (Y_AXIS)[dir];
335 o[Y_AXIS] = col->extent (Y_AXIS)[dir];
339 leave a gap: slur mustn't touch head/stem
341 o[Y_AXIS] += dir * paper_l ()->get_var ("slur_y_free");
342 o[Y_AXIS] -= calc_interstaff_dist (stem_l, this);
347 Slur::after_line_breaking ()
350 set_control_points ();
358 Slur::set_extremities ()
360 Link_array<Note_column> encompass_arr =
361 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
363 if (!encompass_arr.size ())
365 set_elt_property ("transparent", SCM_BOOL_T);
366 set_extent_callback (0, X_AXIS);
367 set_extent_callback (0, Y_AXIS);
371 if (!directional_element (this).get ())
372 directional_element (this).set (get_default_dir ());
376 Slur and tie placement [OSU]
379 * x = centre of head - d * x_gap_f
382 * y = length < 5ss : horizontal tangent + d * 0.25 ss
383 y = length >= 5ss : y next interline - d * 0.25 ss
386 Real staff_space = paper_l ()->get_var ("interline");
387 Real half_staff_space = staff_space / 2;
389 Real x_gap_f = paper_l ()->get_var ("slur_x_gap");
390 Real y_gap_f = paper_l ()->get_var ("slur_y_gap");
392 Drul_array<Note_column*> note_column_drul;
393 note_column_drul[LEFT] = encompass_arr[0];
394 note_column_drul[RIGHT] = encompass_arr.top ();
396 bool fix_broken_b = false;
398 Direction my_dir = directional_element (this).get ();
406 if ((note_column_drul[d] == get_bound (d))
407 && note_column_drul[d]->first_head ()
408 && (note_column_drul[d]->stem_l ()))
410 Stem* stem_l = note_column_drul[d]->stem_l ();
412 side directly attached to note head;
413 no beam getting in the way
415 if ((stem_l->extent (Y_AXIS).empty_b ()
416 || !((stem_l->get_direction () == my_dir) && (my_dir != d)))
417 && !((my_dir == stem_l->get_direction ())
418 && stem_l->beam_l () && (stem_l->beam_count (-d) >= 1)))
420 dx_f_drul_[d] = get_bound (d)->extent (X_AXIS).length () / 2;
421 dx_f_drul_[d] -= d * x_gap_f;
423 if (stem_l->get_direction () != my_dir)
425 dy_f_drul_[d] = note_column_drul[d]->extent (Y_AXIS)[my_dir];
429 dy_f_drul_[d] = stem_l->chord_start_f ()
430 + my_dir * half_staff_space;
432 dy_f_drul_[d] += my_dir * y_gap_f;
435 side attached to (visible) stem
439 dx_f_drul_[d] = stem_l->relative_coordinate (0, X_AXIS)
440 - get_bound (d)->relative_coordinate (0, X_AXIS);
442 side attached to beamed stem
444 if (stem_l->beam_l () && (stem_l->beam_count (-d) >= 1))
446 dy_f_drul_[d] = stem_l->extent (Y_AXIS)[my_dir];
447 dy_f_drul_[d] += my_dir * 2 * y_gap_f;
450 side attached to notehead, with stem getting in the way
454 dx_f_drul_[d] -= d * x_gap_f;
456 dy_f_drul_[d] = stem_l->chord_start_f ()
457 + my_dir * half_staff_space;
458 dy_f_drul_[d] += my_dir * y_gap_f;
467 dx_f_drul_[d] = get_broken_left_end_align ();
470 broken: should get y from other piece, so that slur
471 continues up/down trend
473 for now: be horizontal..
478 while (flip (&d) != LEFT);
480 int cross_count = cross_staff_count ();
481 bool interstaff_b = (0 < cross_count) && (cross_count < encompass_arr.size ());
483 Drul_array<Offset> info_drul;
484 Drul_array<Real> interstaff_interval;
488 info_drul[d] = encompass_offset (encompass_arr.boundary (d, 0));
489 interstaff_interval[d] = - calc_interstaff_dist (encompass_arr.boundary (d,0),
492 while (flip (&d) != LEFT);
494 Real interstaff_f = interstaff_interval[RIGHT] - interstaff_interval[LEFT];
498 Direction d = (encompass_arr.top () != get_bound (RIGHT)) ?
500 dy_f_drul_[d] = info_drul[d][Y_AXIS];
503 dy_f_drul_[d] -= interstaff_interval[d];
504 if (cross_count) // interstaff_i ?
506 dy_f_drul_[LEFT] += interstaff_interval[d];
507 dy_f_drul_[RIGHT] += interstaff_interval[d];
513 dy_f_drul_[RIGHT] += interstaff_f;
518 Slur::cross_staff_count ()const
520 Link_array<Note_column> encompass_arr =
521 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
525 for (int i = 0; i < encompass_arr.size (); i++)
527 if (calc_interstaff_dist (encompass_arr[i], this))
535 Slur::get_encompass_offset_arr () const
537 Link_array<Note_column> encompass_arr =
538 Pointer_group_interface__extract_elements (this, (Note_column*)0, "note-columns");
540 Array<Offset> offset_arr;
544 check non-disturbed slur
545 FIXME: x of ends off by a tiny bit!!
547 offset_arr.push (Offset (0, dy_f_drul_[LEFT]));
548 offset_arr.push (Offset (0, dy_f_drul_[RIGHT]));
552 Offset origin (relative_coordinate (0, X_AXIS), 0);
555 int last = encompass_arr.size () - 2;
557 offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
563 int cross_count = cross_staff_count ();
564 bool cross_b = cross_count && cross_count < encompass_arr.size ();
565 if (encompass_arr[0] != get_bound (LEFT))
568 Real is = calc_interstaff_dist (encompass_arr[0], this);
570 offset_arr[0][Y_AXIS] += is;
576 if (encompass_arr.top () != get_bound (RIGHT))
581 for (int i = first; i <= last; i++)
583 Offset o (encompass_offset (encompass_arr[i]));
584 offset_arr.push (o - origin);
587 offset_arr.push (Offset (spanner_length ()+ dx_f_drul_[RIGHT],
595 Slur::get_rods () const
600 r.item_l_drul_[LEFT] = get_bound (LEFT);
601 r.item_l_drul_[RIGHT] = get_bound (RIGHT);
602 r.distance_f_ = paper_l ()->get_var ("slur_x_minimum");
611 Ugh should have dash-length + dash-period
614 Slur::do_brew_molecule () const
616 Real thick = paper_l ()->get_var ("slur_thickness");
617 Bezier one = get_curve ();
620 SCM d = get_elt_property ("dashed");
622 a = lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
624 a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
630 Slur::set_control_points ()
632 Slur_bezier_bow bb (get_encompass_offset_arr (),
633 directional_element (this).get ());
635 Real staff_space = Staff_symbol_referencer_interface (this).staff_space ();
636 Real h_inf = paper_l ()->get_var ("slur_height_limit_factor") * staff_space;
637 Real r_0 = paper_l ()->get_var ("slur_ratio");
639 bb.set_default_bezier (h_inf, r_0);
641 if (bb.fit_factor () > 1.0)
643 Real length = bb.curve_.control_[3][X_AXIS];
644 Real default_height = bb.get_default_height (h_inf, r_0, length);
645 bb.minimise_enclosed_area (paper_l(), default_height);
647 Real bff = paper_l ()->get_var ("slur_force_blowfit");
648 bb.curve_.control_[1][Y_AXIS] *= bff;
649 bb.curve_.control_[2][Y_AXIS] *= bff;
652 Real sb = paper_l ()->get_var ("slur_beautiful");
653 Real beautiful = length * default_height * sb;
654 Real area = bb.enclosed_area_f ();
657 Slurs that fit beautifully are not ugly
659 if (area > beautiful)
660 de_uglyfy (&bb, default_height);
663 Bezier b = bb.get_bezier ();
666 SCM controls = SCM_EOL;
668 controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
670 set_elt_property ("control-points", controls);
675 Slur::get_curve () const
680 if (!directional_element (this).get ())
681 ((Slur*)this)->set_extremities ();
683 if (!gh_pair_p (get_elt_property ("control-points")))
684 ((Slur*)this)->set_control_points ();
687 for (SCM s= get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
689 b.control_[i] = ly_scm2offset (gh_car (s));
693 Array<Offset> enc (get_encompass_offset_arr ());
694 Direction dir = directional_element (this).get ();
696 Real x1 = enc[0][X_AXIS];
697 Real x2 = enc.top ()[X_AXIS];
700 for (int i=1; i < enc.size ()-1; i++)
702 Real x = enc[i][X_AXIS];
705 Real y = b.get_other_coordinate (X_AXIS, x);
706 off = off >? dir * (enc[i][Y_AXIS] - y);
709 b.translate (Offset (0, dir * off));