2 slur.cc -- implement Slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
13 * should avoid stafflines with horizontal part.
15 * begin and end should be treated as a/acknowledge Scripts.
17 * smart changing of endings
19 * smart changing of (Y-?)offsets to avoid ugly beziers
24 #include "directional-element-interface.hh"
25 #include "group-interface.hh"
28 #include "paper-def.hh"
29 #include "note-column.hh"
31 #include "paper-column.hh"
32 #include "molecule.hh"
34 #include "slur-bezier-bow.hh"
36 #include "group-interface.hh"
37 #include "staff-symbol-referencer.hh"
42 Slur::set_interface (Grob*me)
44 /* Copy to mutable list. */
45 me->set_grob_property ("attachment",
46 ly_deep_copy (me->get_grob_property ("attachment")));
50 Slur::add_column (Grob*me, Grob*n)
52 if (!gh_pair_p (n->get_grob_property ("note-heads")))
53 me->warning (_ ("Putting slur over rest."));
55 Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
56 me->add_dependency (n);
58 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (n));
62 Slur::de_uglyfy (Grob*me, Slur_bezier_bow* bb, Real default_height)
64 Real length = bb->curve_.control_[3][X_AXIS] ;
65 Real ff = bb->fit_factor ();
66 for (int i = 1; i < 3; i++)
68 Real ind = abs (bb->curve_.control_[ (i-1)*3][X_AXIS]
69 - bb->curve_.control_[i][X_AXIS]) / length;
70 Real h = bb->curve_.control_[i][Y_AXIS] * ff / length;
72 Real f = default_height / length;
73 SCM up = me->get_grob_property ("de-uglify-parameters");
75 Real c1 = gh_scm2double (ly_car (up));
76 Real c2 = gh_scm2double (ly_cadr (up));
77 Real c3 = gh_scm2double (ly_caddr (up));
83 else if (h > c2 + c3 * ind)
88 bb->curve_.control_[i][Y_AXIS] = h * length;
91 bb->curve_.assert_sanity ();
97 Slur::get_default_dir (Grob*me)
99 Link_array<Grob> encompass_arr =
100 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
103 for (int i=0; i < encompass_arr.size (); i ++)
105 if (Note_column::dir (encompass_arr[i]) < 0)
115 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking,1);
117 Slur::after_line_breaking (SCM smob)
119 Grob *me = unsmob_grob (smob);
120 if (!scm_ilength (me->get_grob_property ("note-columns")))
123 return SCM_UNSPECIFIED;
125 set_extremities (me);
126 set_control_points (me);
127 return SCM_UNSPECIFIED;
132 Slur::check_slope (Grob *me)
136 Avoid too steep slurs.
138 SCM s = me->get_grob_property ("slope-limit");
141 Array<Offset> encompass = get_encompass_offset_arr (me);
142 Drul_array<Offset> attachment;
143 attachment[LEFT] = encompass[0];
144 attachment[RIGHT] = encompass.top ();
146 Real dx = attachment[RIGHT][X_AXIS] - attachment[LEFT][X_AXIS];
147 Real dy = attachment[RIGHT][Y_AXIS] - attachment[LEFT][Y_AXIS];
151 Real slope = slope = abs (dy / dx);
153 Real limit = gh_scm2double (s);
157 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
158 Direction dir = (Direction)gh_scm2int (me->get_grob_property ("direction"));
159 Direction d = (Direction) (- dir * (sign (dy)));
160 SCM a = me->get_grob_property ("attachment-offset");
161 Drul_array<Offset> o;
162 o[LEFT] = ly_scm2offset (index_cell (a, LEFT));
163 o[RIGHT] = ly_scm2offset (index_cell (a, RIGHT));
164 o[d][Y_AXIS] -= (limit - slope) * dx * dir / staff_space;
166 o[d][Y_AXIS] *= Directional_element_interface::get (me);
168 me->set_grob_property ("attachment-offset",
169 gh_cons (ly_offset2scm (o[LEFT]),
170 ly_offset2scm (o[RIGHT])));
177 Set 'attachment grob property, and return it.
180 Slur::set_extremities (Grob *me)
182 if (!Directional_element_interface::get (me))
183 Directional_element_interface::set (me, get_default_dir (me));
185 SCM att = me->get_grob_property ("attachment");
188 if (!gh_pair_p (att))
190 programming_error ("attachment is not a cons?!");
191 att = gh_cons (SCM_EOL, SCM_EOL);
192 me->set_grob_property ("attachment", att);
195 Direction dir = LEFT;
199 if (!gh_symbol_p (index_cell (att, dir)))
201 for (SCM s = me->get_grob_property ("extremity-rules");
202 s != SCM_EOL; s = ly_cdr (s))
204 SCM r = gh_call2 (ly_caar (s), me->self_scm (),
205 gh_int2scm ((int)dir));
208 index_set_cell (att, dir,
215 while (flip (&dir) != LEFT);
224 Slur::get_first_notecolumn_y (Grob *me, Direction dir)
226 Grob *col = dir == LEFT
227 ? unsmob_grob (ly_car (scm_reverse (me->get_grob_property
230 (ly_car (me->get_grob_property ("note-columns")));
235 me->common_refpoint (col, Y_AXIS)
238 if (col == ((Spanner*)me)->get_bound (dir))
240 y = get_attachment (me, dir, common)[Y_AXIS];
244 y = encompass_offset (me, col, common)[Y_AXIS]
245 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
251 Slur::broken_trend_offset (Grob *me, Direction dir)
254 A broken slur should maintain the same vertical trend
255 the unbroken slur would have had.
258 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_l_))
260 for (int i = dir == LEFT ? 0 : mother->broken_into_l_arr_.size () - 1;
261 dir == LEFT ? i < mother->broken_into_l_arr_.size () : i > 0;
262 dir == LEFT ? i++ : i--)
264 if (mother->broken_into_l_arr_[i - dir] == me)
266 Grob *neighbour = mother->broken_into_l_arr_[i];
268 neighbour->set_grob_property ("direction",
269 me->get_grob_property ("direction"));
270 Real neighbour_y = get_first_notecolumn_y (neighbour, dir);
271 Real y = get_first_notecolumn_y (me, -dir);
272 int neighbour_cols = scm_ilength (neighbour->get_grob_property ("note-columns"));
273 int cols = scm_ilength (me->get_grob_property ("note-columns"));
274 o = Offset (0, (y*neighbour_cols + neighbour_y*cols) /
275 (cols + neighbour_cols));
284 COMMON is size-2 array with common refpoints.
286 UGH: this routine delivers offsets which are *not* relative to COMMON.
289 Slur::get_attachment (Grob *me, Direction dir,
292 SCM s = me->get_grob_property ("attachment");
293 if (!gh_symbol_p (index_cell (s, dir)))
295 s = set_extremities (me);
298 SCM a = dir == LEFT ? ly_car (s) : ly_cdr (s);
299 Spanner*sp = dynamic_cast<Spanner*> (me);
300 String str = ly_symbol2string (a);
301 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
302 Real hs = staff_space / 2.0;
305 int slurdir = gh_scm2int (me->get_grob_property ("direction"));
308 if (Note_column::has_interface (sp->get_bound (dir)))
310 Grob * n =sp->get_bound (dir);
311 if ((stem = Note_column::stem_l (n)))
314 if (Grob *head = Note_column::first_head (n))
315 x_extent = head->extent (head, X_AXIS).length ();
317 x_extent = n->extent (n, X_AXIS).length ();
321 o = Offset (0, Stem::head_positions (stem)
322 [Directional_element_interface::get (me)] * hs);
324 Default position is centered in X, on outer side of head Y
326 o += Offset (0.5 * x_extent,
328 * Directional_element_interface::get (me));
330 else if (str == "alongside-stem")
332 o = Offset (0, Stem::chord_start_y (stem));
334 Default position is on stem X, on outer side of head Y
336 o += Offset (x_extent * (1 + Stem::get_direction (stem)),
338 * Directional_element_interface::get (me));
340 else if (str == "stem")
342 o = Offset (0, Stem::stem_end_position (stem) * hs);
344 Default position is on stem X, at stem end Y
346 Real stem_thickness =
347 gh_scm2double (stem->get_grob_property ("thickness"))
348 * stem->paper_l ()->get_var ("linethickness");
350 x_extent * (1 + Stem::get_direction (stem))
351 - ((dir + 1)/2) * stem_thickness
352 + ((1 - slurdir)/2) * stem_thickness,
358 If we're not a note_column, we can't be anything but a loose-end.
359 But if user has set (attachment . (stem . stem)), our string is
362 Hmm, maybe after-line-breaking should set this to loose-end? */
363 else // if (str == "loose-end")
365 SCM other_a = dir == LEFT ? ly_cdr (s) : ly_car (s);
366 if (ly_symbol2string (other_a) != "loose-end")
367 o = broken_trend_offset (me, dir);
370 SCM alist = me->get_grob_property ("extremity-offset-alist");
371 int stemdir = stem ? Stem::get_direction (stem) : 1;
374 gh_int2scm (stemdir * dir),
375 gh_int2scm (slurdir * dir),
376 SCM_UNDEFINED), alist);
380 Offset off = ly_scm2offset (ly_cdr (l)) * staff_space;
382 off[Y_AXIS] *= Directional_element_interface::get (me);
387 What if get_bound () is not a note-column?
389 if (str != "loose-end"
390 && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
392 o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS)
393 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
396 Offset off = ly_scm2offset (index_cell (me->get_grob_property
397 ("attachment-offset"),
400 off[Y_AXIS] *= Directional_element_interface::get (me);
406 Slur::encompass_offset (Grob*me,
411 Grob* stem_l = unsmob_grob (col->get_grob_property ("stem"));
413 Direction dir = Directional_element_interface::get (me);
417 me->warning (_ ("Slur over rest?"));
418 o[X_AXIS] = col->relative_coordinate (common[X_AXIS], X_AXIS);
419 o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
422 Direction stem_dir = Directional_element_interface::get (stem_l);
423 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
426 Simply set x to middle of notehead
429 if (Grob *head = Note_column::first_head (col))
430 x_extent = head->extent (head, X_AXIS).length ();
432 x_extent = col->extent (col, X_AXIS).length ();
433 o[X_AXIS] -= 0.5 * stem_dir * x_extent;
435 if ((stem_dir == dir)
436 && !stem_l->extent (stem_l, Y_AXIS).empty_b ())
438 o[Y_AXIS] = stem_l->extent (common[Y_AXIS], Y_AXIS)[dir];
442 o[Y_AXIS] = col->extent (common[Y_AXIS], Y_AXIS)[dir];
446 leave a gap: slur mustn't touch head/stem
448 o[Y_AXIS] += dir * gh_scm2double (me->get_grob_property ("y-free")) *
454 Slur::get_encompass_offset_arr (Grob *me)
456 Spanner*sp = dynamic_cast<Spanner*> (me);
457 SCM eltlist = me->get_grob_property ("note-columns");
458 Grob *common[] = {common_refpoint_of_list (eltlist, me, X_AXIS),
459 common_refpoint_of_list (eltlist, me, Y_AXIS)};
462 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
463 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
465 Link_array<Grob> encompass_arr = list_to_grob_array (eltlist);
466 Array<Offset> offset_arr;
468 Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
469 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
472 int last = encompass_arr.size () - 2;
474 offset_arr.push (get_attachment (me, LEFT, common));
479 if (encompass_arr[0] != sp->get_bound (LEFT))
484 offset_arr[0][Y_AXIS] -=
485 encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS)
486 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
492 if (encompass_arr.top () != sp->get_bound (RIGHT))
497 for (int i = first; i <= last; i++)
499 Offset o (encompass_offset (me, encompass_arr[i], common));
500 offset_arr.push (o - origin);
503 offset_arr.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
505 if (encompass_arr[0] != sp->get_bound (LEFT))
507 offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS)
508 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
520 MAKE_SCHEME_CALLBACK (Slur, height, 2);
522 Slur::height (SCM smob, SCM ax)
524 Axis a = (Axis)gh_scm2int (ax);
525 Grob * me = unsmob_grob (smob);
526 assert (a == Y_AXIS);
528 SCM mol = me->get_uncached_molecule ();
529 return ly_interval2scm (unsmob_molecule (mol)->extent (a));
533 Ugh should have dash-length + dash-period
535 MAKE_SCHEME_CALLBACK (Slur, brew_molecule,1);
537 Slur::brew_molecule (SCM smob)
539 Grob * me = unsmob_grob (smob);
540 if (!scm_ilength (me->get_grob_property ("note-columns")))
546 Real thick = me->paper_l ()->get_var ("linethickness") *
547 gh_scm2double (me->get_grob_property ("thickness"));
548 Bezier one = get_curve (me);
550 // get_curve may suicide
551 if (!scm_ilength (me->get_grob_property ("note-columns")))
555 SCM d = me->get_grob_property ("dashed");
557 a = Lookup::dashed_slur (one, thick, thick * gh_scm2double (d));
559 a = Lookup::slur (one, Directional_element_interface::get (me) * thick, thick);
561 return a.smobbed_copy ();
565 Slur::set_control_points (Grob*me)
567 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
569 SCM details = me->get_grob_property ("details");
570 SCM h_inf_scm = scm_assq (ly_symbol2scm ("height-limit"), details);
571 SCM r_0_scm = scm_assq (ly_symbol2scm ("ratio"), details);
573 Real r_0 = gh_scm2double (ly_cdr (r_0_scm));
574 Real h_inf = staff_space * gh_scm2double (ly_cdr (h_inf_scm));
576 Slur_bezier_bow bb (get_encompass_offset_arr (me),
577 Directional_element_interface::get (me),
581 if (bb.fit_factor () > 1.0)
583 Real length = bb.curve_.control_[3][X_AXIS];
584 Real default_height = slur_height (length, h_inf, r_0);
586 SCM ssb = me->get_grob_property ("beautiful");
588 if (gh_number_p (ssb))
589 sb = gh_scm2double (ssb);
591 bb.minimise_enclosed_area (sb, details);
592 SCM sbf = scm_assq (ly_symbol2scm ("force-blowfit"), details);
594 if (gh_pair_p (sbf) && gh_number_p (ly_cdr (sbf)))
595 bff = gh_scm2double (ly_cdr (sbf));
597 bb.curve_.control_[1][Y_AXIS] *= bff;
598 bb.curve_.control_[2][Y_AXIS] *= bff;
602 Real beautiful = length * default_height * sb;
603 Real area = bb.enclosed_area_f ();
606 Slurs that fit beautifully are not ugly
608 if (area > beautiful)
609 de_uglyfy (me, &bb, default_height);
612 Bezier b = bb.get_bezier ();
615 SCM controls = SCM_EOL;
618 controls = gh_cons (ly_offset2scm (b.control_[i]), controls);
621 All these null control-points, where do they all come from?
623 if (i && b.control_[i][X_AXIS] == 0)
630 me->set_grob_property ("control-points", controls);
634 Slur::get_curve (Grob*me)
639 SCM attach = me->get_grob_property ("attachment");
640 if (!gh_pair_p (attach))
641 attach = set_extremities(me);
644 if (!Directional_element_interface::get (me)
645 || ! gh_symbol_p (index_cell (attach, LEFT))
646 || ! gh_symbol_p (index_cell (attach, RIGHT)))
647 set_extremities (me);
649 if (!gh_pair_p (me->get_grob_property ("control-points")))
650 set_control_points (me);
652 // set_control_points may suicide
653 if (!scm_ilength (me->get_grob_property ("note-columns")))
656 for (SCM s= me->get_grob_property ("control-points"); s != SCM_EOL; s = ly_cdr (s))
658 b.control_[i] = ly_scm2offset (ly_car (s));
662 Array<Offset> enc (get_encompass_offset_arr (me));
663 Direction dir = Directional_element_interface::get (me);
665 Real x1 = enc[0][X_AXIS];
666 Real x2 = enc.top ()[X_AXIS];
669 for (int i=1; i < enc.size ()-1; i++)
671 Real x = enc[i][X_AXIS];
674 Real y = b.get_other_coordinate (X_AXIS, x);
675 off = off >? dir * (enc[i][Y_AXIS] - y);
678 b.translate (Offset (0, dir * off));
685 ADD_INTERFACE (Slur,"slur-interface",
687 "slope-limit de-uglify-parameters details attachment direction attachment-offset beautiful y-free control-points extremity-rules extremity-offset-alist thickness dashed");