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>
12 * should avoid stafflines with horizontal part.
13 * begin and end should be treated as a/acknowledge Scripts.
14 * smart changing of endings
15 * smart changing of (Y-?)offsets to avoid ugly beziers
19 #include "directional-element-interface.hh"
20 #include "group-interface.hh"
23 #include "paper-def.hh"
24 #include "note-column.hh"
26 #include "paper-column.hh"
27 #include "molecule.hh"
29 #include "slur-bezier-bow.hh"
31 #include "group-interface.hh"
32 #include "staff-symbol-referencer.hh"
37 Slur::set_interface (Grob*me)
39 /* Copy to mutable list. */
40 me->set_grob_property ("attachment",
41 ly_deep_copy (me->get_grob_property ("attachment")));
45 Slur::add_column (Grob*me, Grob*n)
47 if (!gh_pair_p (n->get_grob_property ("note-heads")))
48 me->warning (_ ("Putting slur over rest. Ignoring."));
51 Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
52 me->add_dependency (n);
55 add_bound_item (dynamic_cast<Spanner*> (me), dynamic_cast<Item*> (n));
59 Slur::de_uglyfy (Grob*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_grob_property ("de-uglify-parameters");
72 Real c1 = gh_scm2double (ly_car (up));
73 Real c2 = gh_scm2double (ly_cadr (up));
74 Real c3 = gh_scm2double (ly_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 (Grob*me)
94 Link_array<Grob> encompass_arr =
95 Pointer_group_interface__extract_grobs (me, (Grob*)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,1);
112 Slur::after_line_breaking (SCM smob)
114 Grob *me = unsmob_grob (smob);
115 if (!scm_ilength (me->get_grob_property ("note-columns")))
118 return SCM_UNSPECIFIED;
120 set_extremities (me);
121 set_control_points (me);
122 return SCM_UNSPECIFIED;
127 Slur::check_slope (Grob *me)
130 Avoid too steep slurs.
132 SCM s = me->get_grob_property ("slope-limit");
135 Array<Offset> encompass = get_encompass_offset_arr (me);
136 Drul_array<Offset> attachment;
137 attachment[LEFT] = encompass[0];
138 attachment[RIGHT] = encompass.top ();
140 Real dx = attachment[RIGHT][X_AXIS] - attachment[LEFT][X_AXIS];
141 Real dy = attachment[RIGHT][Y_AXIS] - attachment[LEFT][Y_AXIS];
145 Real slope = slope = abs (dy / dx);
147 Real limit = gh_scm2double (s);
151 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
152 Direction dir = (Direction)gh_scm2int (me->get_grob_property ("direction"));
153 Direction d = (Direction) (- dir * (sign (dy)));
154 SCM a = me->get_grob_property ("attachment-offset");
155 Drul_array<Offset> o;
156 o[LEFT] = ly_scm2offset (index_cell (a, LEFT));
157 o[RIGHT] = ly_scm2offset (index_cell (a, RIGHT));
158 o[d][Y_AXIS] -= (limit - slope) * dx * dir / staff_space;
160 o[d][Y_AXIS] *= Directional_element_interface::get (me);
162 me->set_grob_property ("attachment-offset",
163 gh_cons (ly_offset2scm (o[LEFT]),
164 ly_offset2scm (o[RIGHT])));
170 Set 'attachment grob property, and return it.
173 Slur::set_extremities (Grob *me)
175 if (!Directional_element_interface::get (me))
176 Directional_element_interface::set (me, get_default_dir (me));
178 SCM att = me->get_grob_property ("attachment");
181 if (!gh_pair_p (att))
183 programming_error ("attachment is not a cons?!");
184 att = gh_cons (SCM_EOL, SCM_EOL);
185 me->set_grob_property ("attachment", att);
188 Direction dir = LEFT;
192 if (!gh_symbol_p (index_cell (att, dir)))
194 for (SCM s = me->get_grob_property ("extremity-rules");
195 s != SCM_EOL; s = ly_cdr (s))
197 SCM r = gh_call2 (ly_caar (s), me->self_scm (),
198 gh_int2scm ((int)dir));
201 index_set_cell (att, dir,
208 while (flip (&dir) != LEFT);
217 Slur::get_first_notecolumn_y (Grob *me, Direction dir)
219 Grob *col = dir == LEFT
220 ? unsmob_grob (ly_car (scm_reverse (me->get_grob_property
223 (ly_car (me->get_grob_property ("note-columns")));
228 me->common_refpoint (col, Y_AXIS)
231 if (col == ((Spanner*)me)->get_bound (dir))
233 y = get_attachment (me, dir, common)[Y_AXIS];
237 y = encompass_offset (me, col, common)[Y_AXIS]
238 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
244 Slur::broken_trend_offset (Grob *me, Direction dir)
247 A broken slur should maintain the same vertical trend
248 the unbroken slur would have had.
251 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_l_))
253 for (int i = dir == LEFT ? 0 : mother->broken_into_l_arr_.size () - 1;
254 dir == LEFT ? i < mother->broken_into_l_arr_.size () : i > 0;
255 dir == LEFT ? i++ : i--)
257 if (mother->broken_into_l_arr_[i - dir] == me)
259 Grob *neighbour = mother->broken_into_l_arr_[i];
261 neighbour->set_grob_property ("direction",
262 me->get_grob_property ("direction"));
263 Real neighbour_y = get_first_notecolumn_y (neighbour, dir);
264 Real y = get_first_notecolumn_y (me, -dir);
265 int neighbour_cols = scm_ilength (neighbour->get_grob_property ("note-columns"));
266 int cols = scm_ilength (me->get_grob_property ("note-columns"));
267 o = Offset (0, (y*neighbour_cols + neighbour_y*cols) /
268 (cols + neighbour_cols));
277 Slur::get_attachment (Grob *me, Direction dir,
280 SCM s = me->get_grob_property ("attachment");
281 if (!gh_symbol_p (index_cell (s, dir)))
283 s = set_extremities (me);
286 SCM a = dir == LEFT ? ly_car (s) : ly_cdr (s);
287 Spanner*sp = dynamic_cast<Spanner*> (me);
288 String str = ly_symbol2string (a);
289 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
290 Real hs = staff_space / 2.0;
293 int slurdir = gh_scm2int (me->get_grob_property ("direction"));
296 if (Note_column::has_interface (sp->get_bound (dir)))
298 Grob * n =sp->get_bound (dir);
299 if ((stem = Note_column::stem_l (n)))
302 if (Grob *head = Note_column::first_head (n))
303 x_extent = head->extent (head, X_AXIS).length ();
305 x_extent = n->extent (n, X_AXIS).length ();
309 o = Offset (0, Stem::head_positions (stem)
310 [Directional_element_interface::get (me)] * hs);
312 Default position is centered in X, on outer side of head Y
314 o += Offset (0.5 * x_extent,
316 * Directional_element_interface::get (me));
318 else if (str == "alongside-stem")
320 o = Offset (0, Stem::chord_start_f (stem));
322 Default position is on stem X, on outer side of head Y
324 o += Offset (x_extent * (1 + Stem::get_direction (stem)),
326 * Directional_element_interface::get (me));
328 else if (str == "stem")
330 o = Offset (0, Stem::stem_end_position (stem) * hs);
332 Default position is on stem X, at stem end Y
334 Real stem_thickness =
335 gh_scm2double (stem->get_grob_property ("thickness"))
336 * stem->paper_l ()->get_var ("stafflinethickness");
338 x_extent * (1 + Stem::get_direction (stem))
339 - ((dir + 1)/2) * stem_thickness
340 + ((1 - slurdir)/2) * stem_thickness,
346 If we're not a note_column, we can't be anything but a loose-end.
347 But if user has set (attachment . (stem . stem)), our string is
350 Hmm, maybe after-line-breaking should set this to loose-end? */
351 else // if (str == "loose-end")
353 SCM other_a = dir == LEFT ? ly_cdr (s) : ly_car (s);
354 if (ly_symbol2string (other_a) != "loose-end")
355 o = broken_trend_offset (me, dir);
358 SCM alist = me->get_grob_property ("extremity-offset-alist");
359 int stemdir = stem ? Stem::get_direction (stem) : 1;
362 gh_int2scm (stemdir * dir),
363 gh_int2scm (slurdir * dir),
364 SCM_UNDEFINED), alist);
368 Offset off = ly_scm2offset (ly_cdr (l)) * staff_space;
370 off[Y_AXIS] *= Directional_element_interface::get (me);
375 What if get_bound () is not a note-column?
377 if (str != "loose-end"
378 && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
380 o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS)
381 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
384 Offset off = ly_scm2offset (index_cell (me->get_grob_property
385 ("attachment-offset"),
388 off[Y_AXIS] *= Directional_element_interface::get (me);
394 Slur::encompass_offset (Grob*me,
399 Grob* stem_l = unsmob_grob (col->get_grob_property ("stem"));
401 Direction dir = Directional_element_interface::get (me);
405 me->warning (_ ("Slur over rest?"));
406 o[X_AXIS] = col->relative_coordinate (common[X_AXIS], X_AXIS);
407 o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
410 Direction stem_dir = Directional_element_interface::get (stem_l);
411 o[X_AXIS] = stem_l->relative_coordinate (0, X_AXIS);
414 Simply set x to middle of notehead
417 if (Grob *head = Note_column::first_head (col))
418 x_extent = head->extent (head, X_AXIS).length ();
420 x_extent = col->extent (col, X_AXIS).length ();
421 o[X_AXIS] -= 0.5 * stem_dir * x_extent;
423 if ((stem_dir == dir)
424 && !stem_l->extent (stem_l, Y_AXIS).empty_b ())
426 o[Y_AXIS] = stem_l->extent (common[Y_AXIS], Y_AXIS)[dir];
430 o[Y_AXIS] = col->extent (common[Y_AXIS], Y_AXIS)[dir];
434 leave a gap: slur mustn't touch head/stem
436 o[Y_AXIS] += dir * gh_scm2double (me->get_grob_property ("y-free")) *
442 Slur::get_encompass_offset_arr (Grob *me)
444 Spanner*sp = dynamic_cast<Spanner*> (me);
445 SCM eltlist = me->get_grob_property ("note-columns");
446 Grob *common[] = {me->common_refpoint (eltlist, X_AXIS),
447 me->common_refpoint (eltlist, Y_AXIS)};
450 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
451 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
453 Link_array<Grob> encompass_arr;
454 while (gh_pair_p (eltlist))
456 encompass_arr.push (unsmob_grob (ly_car (eltlist)));
457 eltlist =ly_cdr (eltlist);
459 encompass_arr.reverse ();
462 Array<Offset> offset_arr;
464 Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
465 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
468 int last = encompass_arr.size () - 2;
470 offset_arr.push (get_attachment (me, LEFT, common));
476 if (encompass_arr[0] != sp->get_bound (LEFT))
481 offset_arr[0][Y_AXIS] -=
482 encompass_arr[0]->relative_coordinate (common[Y_AXIS], Y_AXIS)
483 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
489 if (encompass_arr.top () != sp->get_bound (RIGHT))
494 for (int i = first; i <= last; i++)
496 Offset o (encompass_offset (me, encompass_arr[i], common));
497 offset_arr.push (o - origin);
500 offset_arr.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
502 if (encompass_arr[0] != sp->get_bound (LEFT))
504 offset_arr.top ()[Y_AXIS] -= encompass_arr.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS)
505 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
517 MAKE_SCHEME_CALLBACK (Slur, height, 2);
519 Slur::height (SCM smob, SCM ax)
521 Axis a = (Axis)gh_scm2int (ax);
522 Grob * me = unsmob_grob (smob);
523 assert (a == Y_AXIS);
525 SCM mol = me->get_uncached_molecule ();
526 return ly_interval2scm (unsmob_molecule (mol)->extent (a));
530 Ugh should have dash-length + dash-period
532 MAKE_SCHEME_CALLBACK (Slur, brew_molecule,1);
534 Slur::brew_molecule (SCM smob)
536 Grob * me = unsmob_grob (smob);
537 if (!scm_ilength (me->get_grob_property ("note-columns")))
543 Real thick = me->paper_l ()->get_var ("stafflinethickness") *
544 gh_scm2double (me->get_grob_property ("thickness"));
545 Bezier one = get_curve (me);
547 // get_curve may suicide
548 if (!scm_ilength (me->get_grob_property ("note-columns")))
552 SCM d = me->get_grob_property ("dashed");
554 a = Lookup::dashed_slur (one, thick, thick * gh_scm2double (d));
556 a = Lookup::slur (one, Directional_element_interface::get (me) * thick, thick);
558 return a.smobbed_copy ();
562 Slur::set_control_points (Grob*me)
564 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
566 SCM details = me->get_grob_property ("details");
567 SCM h_inf_scm = scm_assq (ly_symbol2scm ("height-limit"), details);
568 SCM r_0_scm = scm_assq (ly_symbol2scm ("ratio"), details);
570 Real r_0 = gh_scm2double (ly_cdr (r_0_scm));
571 Real h_inf = staff_space * gh_scm2double (ly_cdr (h_inf_scm));
573 Slur_bezier_bow bb (get_encompass_offset_arr (me),
574 Directional_element_interface::get (me),
577 if (bb.fit_factor () > 1.0)
579 Real length = bb.curve_.control_[3][X_AXIS];
580 Real default_height = slur_height (length, h_inf, r_0);
582 SCM ssb = me->get_grob_property ("beautiful");
584 if (gh_number_p (ssb))
585 sb = gh_scm2double (ssb);
587 bb.minimise_enclosed_area (sb, details);
588 SCM sbf = scm_assq (ly_symbol2scm ("force-blowfit"), details);
590 if (gh_pair_p (sbf) && gh_number_p (ly_cdr (sbf)))
591 bff = gh_scm2double (ly_cdr (sbf));
593 bb.curve_.control_[1][Y_AXIS] *= bff;
594 bb.curve_.control_[2][Y_AXIS] *= bff;
598 Real beautiful = length * default_height * sb;
599 Real area = bb.enclosed_area_f ();
602 Slurs that fit beautifully are not ugly
604 if (area > beautiful)
605 de_uglyfy (me, &bb, default_height);
608 Bezier b = bb.get_bezier ();
611 SCM controls = SCM_EOL;
614 controls = gh_cons (ly_offset2scm (b.control_[i]), controls);
617 All these null control-points, where do they all come from?
619 if (i && b.control_[i][X_AXIS] == 0)
626 me->set_grob_property ("control-points", controls);
630 Slur::get_curve (Grob*me)
635 SCM attach = me->get_grob_property ("attachment");
636 if (!gh_pair_p (attach))
637 attach = set_extremities(me);
640 if (!Directional_element_interface::get (me)
641 || ! gh_symbol_p (index_cell (attach, LEFT))
642 || ! gh_symbol_p (index_cell (attach, RIGHT)))
643 set_extremities (me);
645 if (!gh_pair_p (me->get_grob_property ("control-points")))
646 set_control_points (me);
648 // set_control_points may suicide
649 if (!scm_ilength (me->get_grob_property ("note-columns")))
652 for (SCM s= me->get_grob_property ("control-points"); s != SCM_EOL; s = ly_cdr (s))
654 b.control_[i] = ly_scm2offset (ly_car (s));
658 Array<Offset> enc (get_encompass_offset_arr (me));
659 Direction dir = Directional_element_interface::get (me);
661 Real x1 = enc[0][X_AXIS];
662 Real x2 = enc.top ()[X_AXIS];
665 for (int i=1; i < enc.size ()-1; i++)
667 Real x = enc[i][X_AXIS];
670 Real y = b.get_other_coordinate (X_AXIS, x);
671 off = off >? dir * (enc[i][Y_AXIS] - y);
674 b.translate (Offset (0, dir * off));
680 Slur::has_interface (Grob*me)
682 return me->has_interface (ly_symbol2scm ("slur-interface"));