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 * fix broken interstaff slurs
13 * begin and end should be treated as a/acknowledge Scripts.
14 * broken slur should have uniform trend
15 * smart changing of endings
16 * smart changing of (Y-?)offsets to avoid ugly beziers
20 #include "directional-element-interface.hh"
21 #include "group-interface.hh"
24 #include "paper-def.hh"
25 #include "note-column.hh"
27 #include "paper-column.hh"
28 #include "molecule.hh"
30 #include "slur-bezier-bow.hh"
32 #include "group-interface.hh"
33 #include "staff-symbol-referencer.hh"
38 Slur::set_interface (Score_element*me)
40 me->set_elt_property ("attachment", gh_cons (SCM_BOOL_F, SCM_BOOL_F));
41 me->set_interface (ly_symbol2scm ("slur-interface"));
45 Slur::add_column (Score_element*me, Score_element*n)
47 if (!gh_pair_p (n->get_elt_property ("note-heads")))
48 warning (_ ("Putting slur over rest. Ignoring."));
51 Pointer_group_interface (me, "note-columns").add_element (n);
52 me->add_dependency (n);
55 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*>(n));
59 Slur::de_uglyfy (Score_element*me, Slur_bezier_bow* bb, Real default_height)
61 Real length = bb->curve_.control_[3][X_AXIS] ;
62 Real ff = bb->fit_factor ();
63 for (int i = 1; i < 3; i++)
65 Real ind = abs (bb->curve_.control_[(i-1)*3][X_AXIS]
66 - bb->curve_.control_[i][X_AXIS]) / length;
67 Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
69 Real f = default_height / length;
70 SCM up = me->get_elt_property ("de-uglify-parameters");
72 Real c1 = gh_scm2double (gh_car (up));
73 Real c2 = gh_scm2double (gh_cadr (up));
74 Real c3 = gh_scm2double (gh_caddr (up));
80 else if (h > c2 + c3 * ind)
85 bb->curve_.control_[i][Y_AXIS] = h * length;
88 bb->curve_.assert_sanity ();
92 Slur::get_default_dir (Score_element*me)
94 Link_array<Score_element> encompass_arr =
95 Pointer_group_interface__extract_elements (me, (Score_element*)0, "note-columns");
98 for (int i=0; i < encompass_arr.size (); i ++)
100 if (Note_column::dir (encompass_arr[i]) < 0)
110 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking);
112 Slur::after_line_breaking (SCM smob)
114 Score_element *me = unsmob_element (smob);
115 if (!scm_ilength (me->get_elt_property ("note-columns")))
118 return SCM_UNSPECIFIED;
120 set_extremities (me);
121 set_control_points (me);
122 return SCM_UNSPECIFIED;
126 Slur::set_extremities (Score_element*me)
128 if (!Directional_element_interface::get (me))
129 Directional_element_interface ::set (me,get_default_dir (me));
131 Direction dir = LEFT;
134 if (!gh_symbol_p (index_cell (me->get_elt_property ("attachment"), dir)))
136 for (SCM s = me->get_elt_property ("extremity-rules");
137 s != SCM_EOL; s = gh_cdr (s))
139 SCM r = gh_call2 (gh_caar (s), me->self_scm (),
140 gh_int2scm ((int)dir));
143 index_set_cell (me->get_elt_property ("attachment"), dir,
150 while (flip (&dir) != LEFT);
154 Slur::get_first_notecolumn_y (Score_element *me, Direction dir)
156 Score_element *col = dir == LEFT
157 ? unsmob_element (gh_car (scm_reverse (me->get_elt_property
160 (gh_car (me->get_elt_property ("note-columns")));
162 Score_element *common[] =
165 me->common_refpoint (col, Y_AXIS)
168 if (col == ((Spanner*)me)->get_bound (dir))
170 y = get_attachment (me, dir, common)[Y_AXIS];
174 y = encompass_offset (me, col, common)[Y_AXIS]
175 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
181 Slur::broken_trend_offset (Score_element *me, Direction dir)
184 A broken slur should maintain the same vertical trend
185 the unbroken slur would have had.
188 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_l_))
190 for (int i = dir == LEFT ? 0 : mother->broken_into_l_arr_.size () - 1;
191 dir == LEFT ? i < mother->broken_into_l_arr_.size () : i > 0;
192 dir == LEFT ? i++ : i--)
194 if (mother->broken_into_l_arr_[i - dir] == me)
196 Score_element *neighbour = mother->broken_into_l_arr_[i];
198 neighbour->set_elt_property ("direction",
199 me->get_elt_property ("direction"));
200 Real neighbour_y = get_first_notecolumn_y (neighbour, dir);
201 Real y = get_first_notecolumn_y (me, -dir);
202 o = Offset (0, (y + neighbour_y) / 2);
211 Slur::get_attachment (Score_element*me,Direction dir,
212 Score_element **common)
214 SCM s = me->get_elt_property ("attachment");
215 if (!gh_symbol_p (index_cell (s, dir)))
217 set_extremities (me);
218 s = me->get_elt_property ("attachment");
220 SCM a = dir == LEFT ? gh_car (s) : gh_cdr (s);
221 Spanner*sp = dynamic_cast<Spanner*>(me);
222 String str = ly_symbol2string (a);
223 Real ss = Staff_symbol_referencer::staff_space ((Score_element*)me);
227 Score_element *stem = 0;
228 if (Note_column::has_interface (sp->get_bound (dir)))
230 Score_element * n =sp->get_bound (dir);
231 if ((stem = Note_column::stem_l (n)))
236 o = Offset (0, Stem::head_positions (stem)
237 [Directional_element_interface::get (me)] * hs);
239 Default position is centered in X, on outer side of head Y
241 o += Offset (0.5 * n->extent (X_AXIS).length (),
242 0.5 * ss * Directional_element_interface::get (me));
244 else if (str == "alongside-stem")
246 o = Offset (0, Stem::chord_start_f (stem));
248 Default position is on stem X, on outer side of head Y
250 o += Offset (n->extent (X_AXIS).length ()
251 * (1 + Stem::get_direction (stem)),
252 0.5 * ss * Directional_element_interface::get (me));
254 else if (str == "stem")
256 o = Offset (0, Stem::stem_end_position (stem) * hs);
258 Default position is on stem X, at stem end Y
261 (n->extent (X_AXIS).length ()
262 - stem->extent (X_AXIS).length ())
263 * (1 + Stem::get_direction (stem)),
268 else if (str == "loose-end")
270 SCM other_a = dir == LEFT ? gh_cdr (s) : gh_car (s);
271 if (ly_symbol2string (other_a) != "loose-end")
275 The braindead way: horizontal
277 o = Offset (0, get_attachment (me, -dir, common)[Y_AXIS]);
279 o = broken_trend_offset (me, dir);
287 SCM alist = me->get_elt_property ("extremity-offset-alist");
288 int stemdir = stem ? Stem::get_direction (stem) : 1;
289 int slurdir = gh_scm2int (me->get_elt_property ("direction"));
292 gh_int2scm (stemdir * dir),
293 gh_int2scm (slurdir * dir),
294 SCM_UNDEFINED), alist);
298 o += ly_scm2offset (gh_cdr (l)) * ss * dir;
302 What if get_bound () is not a note-column?
304 if (str != "loose-end"
305 && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
307 o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS)
308 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
315 Slur::encompass_offset (Score_element*me,
317 Score_element **common)
320 Score_element* stem_l = unsmob_element (col->get_elt_property ("stem"));
322 Direction dir = Directional_element_interface::get (me);
326 warning (_ ("Slur over rest?"));
327 o[X_AXIS] = col->relative_coordinate (common[X_AXIS], X_AXIS);
328 o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
331 Direction stem_dir = Directional_element_interface::get (stem_l);
332 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
335 Simply set x to middle of notehead
338 o[X_AXIS] -= 0.5 * stem_dir * col->extent (X_AXIS).length ();
340 if ((stem_dir == dir)
341 && !stem_l->extent (Y_AXIS).empty_b ())
343 o[Y_AXIS] = stem_l->relative_coordinate (common[Y_AXIS], Y_AXIS); // iuhg
344 o[Y_AXIS] += stem_l->extent (Y_AXIS)[dir];
348 o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS); // ugh
349 o[Y_AXIS] += col->extent (Y_AXIS)[dir];
353 leave a gap: slur mustn't touch head/stem
355 o[Y_AXIS] += dir * gh_scm2double (me->get_elt_property ("y-free")) *
356 me->paper_l ()->get_var ("staffspace");
361 Slur::get_encompass_offset_arr (Score_element*me)
363 Spanner*sp = dynamic_cast<Spanner*>(me);
364 SCM eltlist = me->get_elt_property ("note-columns");
365 Score_element *common[] = {me->common_refpoint (eltlist,X_AXIS),
366 me->common_refpoint (eltlist,Y_AXIS)};
369 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
370 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
372 Link_array<Score_element> encompass_arr;
373 while (gh_pair_p (eltlist))
375 encompass_arr.push (unsmob_element (gh_car (eltlist)));
376 eltlist =gh_cdr (eltlist);
378 encompass_arr.reverse ();
381 Array<Offset> offset_arr;
383 Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
384 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
387 int last = encompass_arr.size () - 2;
389 offset_arr.push (get_attachment (me, LEFT, common));
395 if (encompass_arr[0] != sp->get_bound (LEFT))
400 offset_arr[0][Y_AXIS] -=
401 encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS)
402 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
408 if (encompass_arr.top () != sp->get_bound (RIGHT))
413 for (int i = first; i <= last; i++)
415 Offset o (encompass_offset (me, encompass_arr[i], common));
416 offset_arr.push (o - origin);
419 offset_arr.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
421 if (encompass_arr[0] != sp->get_bound (LEFT))
423 offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS)
424 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
431 MAKE_SCHEME_CALLBACK(Slur,set_spacing_rods);
433 Slur::set_spacing_rods (SCM smob)
435 Score_element*me = unsmob_element (smob);
438 Spanner*sp = dynamic_cast<Spanner*>(me);
439 r.item_l_drul_[LEFT] = sp->get_bound (LEFT);
440 r.item_l_drul_[RIGHT] = sp->get_bound (RIGHT);
442 gh_scm2double (me->get_elt_property ("minimum-length"))
443 * me->paper_l ()->get_var ("staffspace");
446 return SCM_UNSPECIFIED;
451 Ugh should have dash-length + dash-period
453 MAKE_SCHEME_CALLBACK (Slur, brew_molecule);
455 Slur::brew_molecule (SCM smob)
457 Score_element * me = unsmob_element (smob);
458 if (!scm_ilength (me->get_elt_property ("note-columns")))
464 Real thick = me->paper_l ()->get_var ("stafflinethickness") *
465 gh_scm2double (me->get_elt_property ("thickness"));
466 Bezier one = get_curve (me);
469 SCM d = me->get_elt_property ("dashed");
471 a = me->lookup_l ()->dashed_slur (one, thick, thick * gh_scm2double (d));
473 a = me->lookup_l ()->slur (one, Directional_element_interface::get (me) * thick, thick);
475 return a.create_scheme();
479 Slur::set_control_points (Score_element*me)
481 Real staff_space = Staff_symbol_referencer::staff_space ((Score_element*)me);
483 SCM details = me->get_elt_property ("details");
484 SCM h_inf_scm = scm_assq (ly_symbol2scm ("height-limit"), details);
485 SCM r_0_scm = scm_assq (ly_symbol2scm ("ratio"), details);
487 Real r_0 = gh_scm2double (gh_cdr (r_0_scm));
488 Real h_inf = staff_space * gh_scm2double (gh_cdr (h_inf_scm));
490 Slur_bezier_bow bb (get_encompass_offset_arr (me),
491 Directional_element_interface::get (me),
494 if (bb.fit_factor () > 1.0)
496 Real length = bb.curve_.control_[3][X_AXIS];
497 Real default_height = slur_height (length, h_inf, r_0);
499 SCM ssb = scm_assq (ly_symbol2scm ("beautiful"), details);
500 Real sb =gh_scm2double (gh_cdr (ssb));
502 bb.minimise_enclosed_area (me->paper_l(), sb);
503 SCM sbf = scm_assq (ly_symbol2scm ("force-blowfit"), details);
505 if (gh_pair_p (sbf) && gh_number_p (gh_cdr (sbf)))
506 bff = gh_scm2double (gh_cdr (sbf));
508 bb.curve_.control_[1][Y_AXIS] *= bff;
509 bb.curve_.control_[2][Y_AXIS] *= bff;
513 Real beautiful = length * default_height * sb;
514 Real area = bb.enclosed_area_f ();
517 Slurs that fit beautifully are not ugly
519 if (area > beautiful)
520 de_uglyfy (me, &bb, default_height);
523 Bezier b = bb.get_bezier ();
526 SCM controls = SCM_EOL;
529 controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
532 All these null control-points, where do they all come from?
534 if (i && b.control_[i][X_AXIS] == 0)
538 me->set_elt_property ("control-points", controls);
542 Slur::get_curve (Score_element*me)
547 if (!Directional_element_interface::get (me)
548 || ! gh_symbol_p (index_cell (me->get_elt_property ("attachment"), LEFT)))
549 set_extremities (me);
551 if (!gh_pair_p (me->get_elt_property ("control-points")))
552 set_control_points (me);
554 for (SCM s= me->get_elt_property ("control-points"); s != SCM_EOL; s = gh_cdr (s))
556 b.control_[i] = ly_scm2offset (gh_car (s));
560 Array<Offset> enc (get_encompass_offset_arr (me));
561 Direction dir = Directional_element_interface::get (me);
563 Real x1 = enc[0][X_AXIS];
564 Real x2 = enc.top ()[X_AXIS];
567 for (int i=1; i < enc.size ()-1; i++)
569 Real x = enc[i][X_AXIS];
572 Real y = b.get_other_coordinate (X_AXIS, x);
573 off = off >? dir * (enc[i][Y_AXIS] - y);
576 b.translate (Offset (0, dir * off));
582 Slur::has_interface (Score_element*me)
584 return me->has_interface (ly_symbol2scm ("slur-interface"));