2 slur.cc -- implement Slur
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2003 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 Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
53 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 ();
94 Slur::get_default_dir (Grob*me)
96 Link_array<Grob> encompasses =
97 Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
100 for (int i=0; i < encompasses.size (); i ++)
102 if (Note_column::dir (encompasses[i]) < 0)
112 MAKE_SCHEME_CALLBACK (Slur, after_line_breaking,1);
114 Slur::after_line_breaking (SCM smob)
116 Grob *me = unsmob_grob (smob);
117 if (!scm_ilength (me->get_grob_property ("note-columns")))
120 return SCM_UNSPECIFIED;
122 set_extremities (me);
123 set_control_points (me);
124 return SCM_UNSPECIFIED;
129 Slur::check_slope (Grob *me)
133 Avoid too steep slurs.
135 SCM s = me->get_grob_property ("slope-limit");
138 Array<Offset> encompass = get_encompass_offsets (me);
139 Drul_array<Offset> attachment;
140 attachment[LEFT] = encompass[0];
141 attachment[RIGHT] = encompass.top ();
143 Real dx = attachment[RIGHT][X_AXIS] - attachment[LEFT][X_AXIS];
144 Real dy = attachment[RIGHT][Y_AXIS] - attachment[LEFT][Y_AXIS];
148 Real slope = slope = abs (dy / dx);
150 Real limit = gh_scm2double (s);
154 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
155 Direction dir = (Direction)gh_scm2int (me->get_grob_property ("direction"));
156 Direction d = (Direction) (- dir * (sign (dy)));
157 SCM a = me->get_grob_property ("attachment-offset");
158 Drul_array<Offset> o;
159 o[LEFT] = ly_scm2offset (index_get_cell (a, LEFT));
160 o[RIGHT] = ly_scm2offset (index_get_cell (a, RIGHT));
161 o[d][Y_AXIS] -= (limit - slope) * dx * dir / staff_space;
163 o[d][Y_AXIS] *= Directional_element_interface::get (me);
165 me->set_grob_property ("attachment-offset",
166 gh_cons (ly_offset2scm (o[LEFT]),
167 ly_offset2scm (o[RIGHT])));
174 Set 'attachment grob property, and return it.
177 Slur::set_extremities (Grob *me)
179 if (!Directional_element_interface::get (me))
180 Directional_element_interface::set (me, get_default_dir (me));
182 SCM att = me->get_grob_property ("attachment");
185 if (!gh_pair_p (att))
187 programming_error ("attachment is not a cons?!");
188 att = gh_cons (SCM_EOL, SCM_EOL);
189 me->set_grob_property ("attachment", att);
192 Direction dir = LEFT;
196 if (!gh_symbol_p (index_get_cell (att, dir)))
198 for (SCM s = me->get_grob_property ("extremity-rules");
199 s != SCM_EOL; s = ly_cdr (s))
201 SCM r = gh_call2 (ly_caar (s), me->self_scm (),
202 gh_int2scm ((int)dir));
205 index_set_cell (att, dir,
212 while (flip (&dir) != LEFT);
221 Slur::get_boundary_notecolumn_y (Grob *me, Direction dir)
223 SCM cols = me->get_grob_property ("note-columns");
225 if(!gh_pair_p (cols))
227 programming_error ("No note-columns in slur?");
233 cols = scm_reverse(cols);
235 Grob *col = unsmob_grob (ly_car (cols));
239 me->common_refpoint (col, Y_AXIS)
242 if (col == ((Spanner*)me)->get_bound (dir))
244 y = get_attachment (me, dir, common)[Y_AXIS];
248 y = encompass_offset (me, col, common)[Y_AXIS]
249 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
255 Slur::broken_trend_offset (Grob *me, Direction dir)
258 A broken slur should maintain the same vertical trend
259 the unbroken slur would have had.
262 if (Spanner *mother = dynamic_cast<Spanner*> (me->original_))
264 int k = broken_spanner_index (dynamic_cast<Spanner*> (me));
265 Grob *neighbour = mother->broken_intos_[k + dir];
267 neighbour->set_grob_property ("direction",
268 me->get_grob_property ("direction"));
269 Real neighbour_y = get_boundary_notecolumn_y (neighbour, dir);
270 Real y = get_boundary_notecolumn_y (me, -dir);
271 int neighbour_cols = scm_ilength (neighbour->get_grob_property ("note-columns"));
272 int cols = scm_ilength (me->get_grob_property ("note-columns"));
273 o = Offset (0, (y*neighbour_cols + neighbour_y*cols) /
274 (cols + neighbour_cols));
280 COMMON is size-2 array with common refpoints.
282 UGH: this routine delivers offsets which are *not* relative to COMMON.
284 UGH, we should take COMMON-Y as argument.
287 Slur::get_attachment (Grob *me, Direction dir,
290 SCM s = me->get_grob_property ("attachment");
291 if (!gh_symbol_p (index_get_cell (s, dir)))
293 s = set_extremities (me);
296 SCM a = dir == LEFT ? ly_car (s) : ly_cdr (s);
297 Spanner*sp = dynamic_cast<Spanner*> (me);
298 String str = ly_symbol2string (a);
299 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
300 Real hs = staff_space / 2.0;
303 int slurdir = gh_scm2int (me->get_grob_property ("direction"));
306 if (Note_column::has_interface (sp->get_bound (dir)))
308 Grob * n =sp->get_bound (dir);
309 if ((stem = Note_column::get_stem (n)))
312 if (Grob *head = Note_column::first_head (n))
313 x_extent = head->extent (head, X_AXIS).length ();
315 x_extent = n->extent (n, X_AXIS).length ();
319 o = Offset (0, Stem::head_positions (stem)
320 [Directional_element_interface::get (me)] * hs);
322 Default position is centered in X, on outer side of head Y
324 o += Offset (0.5 * x_extent,
326 * Directional_element_interface::get (me));
328 else if (str == "alongside-stem")
330 o = Offset (0, Stem::chord_start_y (stem));
332 Default position is on stem X, on outer side of head Y
334 o += Offset (x_extent * (1 + Stem::get_direction (stem)),
336 * Directional_element_interface::get (me));
338 else if (str == "stem")
340 o = Offset (0, Stem::stem_end_position (stem) * hs);
342 Default position is on stem X, at stem end Y
344 Real stem_thickness =
345 gh_scm2double (stem->get_grob_property ("thickness"))
346 * stem->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));
348 x_extent * (1 + Stem::get_direction (stem))
349 - ((dir + 1)/2) * stem_thickness
350 + ((1 - slurdir)/2) * stem_thickness,
356 If we're not a note_column, we can't be anything but a loose-end.
357 But if user has set (attachment . (stem . stem)), our string is
360 Hmm, maybe after-line-breaking should set this to loose-end? */
361 else // if (str == "loose-end")
363 SCM other_a = dir == LEFT ? ly_cdr (s) : ly_car (s);
364 if (ly_symbol2string (other_a) != "loose-end")
365 o = broken_trend_offset (me, dir);
368 SCM alist = me->get_grob_property ("extremity-offset-alist");
369 int stemdir = stem ? Stem::get_direction (stem) : 1;
372 gh_int2scm (stemdir * dir),
373 gh_int2scm (slurdir * dir),
374 SCM_UNDEFINED), alist);
378 Offset off = ly_scm2offset (ly_cdr (l)) * staff_space;
380 off[Y_AXIS] *= Directional_element_interface::get (me);
385 What if get_bound () is not a note-column?
387 if (str != "loose-end"
388 && sp->get_bound (dir)->common_refpoint (common[Y_AXIS], Y_AXIS) == common[Y_AXIS])
390 o[Y_AXIS] += sp->get_bound (dir)->relative_coordinate (common[Y_AXIS], Y_AXIS)
391 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
394 Offset off = ly_scm2offset (index_get_cell (me->get_grob_property
395 ("attachment-offset"),
398 off[Y_AXIS] *= Directional_element_interface::get (me);
404 Slur::encompass_offset (Grob*me,
409 Grob* stem = unsmob_grob (col->get_grob_property ("stem"));
411 Direction dir = Directional_element_interface::get (me);
415 programming_error ("No stem for note column?");
416 o[X_AXIS] = col->relative_coordinate (common[X_AXIS], X_AXIS);
417 o[Y_AXIS] = col->relative_coordinate (common[Y_AXIS], Y_AXIS);
420 Direction stem_dir = Directional_element_interface::get (stem);
421 o[X_AXIS] = stem->relative_coordinate (0, X_AXIS);
424 Simply set x to middle of notehead
427 if (Grob *head = Note_column::first_head (col))
428 x_extent = head->extent (head, X_AXIS).length ();
430 x_extent = col->extent (col, X_AXIS).length ();
431 o[X_AXIS] -= 0.5 * stem_dir * x_extent;
433 if ((stem_dir == dir)
434 && !stem->extent (stem, Y_AXIS).empty_b ())
436 o[Y_AXIS] = stem->extent (common[Y_AXIS], Y_AXIS)[dir];
440 o[Y_AXIS] = col->extent (common[Y_AXIS], Y_AXIS)[dir];
444 leave a gap: slur mustn't touch head/stem
446 o[Y_AXIS] += dir * gh_scm2double (me->get_grob_property ("y-free")) *
452 Slur::get_encompass_offsets (Grob *me)
454 Spanner*sp = dynamic_cast<Spanner*> (me);
455 SCM eltlist = me->get_grob_property ("note-columns");
456 Grob *common[] = {common_refpoint_of_list (eltlist, me, X_AXIS),
457 common_refpoint_of_list (eltlist, me, Y_AXIS)};
460 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (RIGHT), X_AXIS);
461 common[X_AXIS] = common[X_AXIS]->common_refpoint (sp->get_bound (LEFT), X_AXIS);
463 Link_array<Grob> encompasses = ly_scm2grobs (eltlist);
464 Array<Offset> offsets;
466 Offset origin (me->relative_coordinate (common[X_AXIS], X_AXIS),
467 me->relative_coordinate (common[Y_AXIS], Y_AXIS));
470 int last = encompasses.size () - 2;
472 offsets.push (get_attachment (me, LEFT, common));
477 if (encompasses[0] != sp->get_bound (LEFT))
482 offsets[0][Y_AXIS] -=
483 encompasses[0]->relative_coordinate (common[Y_AXIS], Y_AXIS)
484 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
490 if (encompasses.top () != sp->get_bound (RIGHT))
495 for (int i = first; i <= last; i++)
497 Offset o (encompass_offset (me, encompasses[i], common));
498 offsets.push (o - origin);
501 offsets.push (Offset (sp->spanner_length (), 0) + get_attachment (me, RIGHT,common));
503 if (encompasses[0] != sp->get_bound (LEFT))
505 offsets.top ()[Y_AXIS] -= encompasses.top ()->relative_coordinate (common[Y_AXIS], Y_AXIS)
506 - me->relative_coordinate (common[Y_AXIS], Y_AXIS);
518 MAKE_SCHEME_CALLBACK (Slur, height, 2);
520 Slur::height (SCM smob, SCM ax)
522 Axis a = (Axis)gh_scm2int (ax);
523 Grob * me = unsmob_grob (smob);
524 assert (a == Y_AXIS);
526 SCM mol = me->get_uncached_molecule ();
527 return ly_interval2scm (unsmob_molecule (mol)->extent (a));
531 Ugh should have dash-length + dash-period
533 MAKE_SCHEME_CALLBACK (Slur, brew_molecule,1);
535 Slur::brew_molecule (SCM smob)
537 Grob * me = unsmob_grob (smob);
538 if (!scm_ilength (me->get_grob_property ("note-columns")))
544 Real thick = me->get_paper ()->get_realvar (ly_symbol2scm ("linethickness")) *
545 gh_scm2double (me->get_grob_property ("thickness"));
546 Bezier one = get_curve (me);
548 // get_curve may suicide
549 if (!scm_ilength (me->get_grob_property ("note-columns")))
553 SCM d = me->get_grob_property ("dashed");
555 a = Lookup::dashed_slur (one, thick, thick * gh_scm2double (d));
557 a = Lookup::slur (one, Directional_element_interface::get (me) * thick, thick);
559 return a.smobbed_copy ();
563 Slur::set_control_points (Grob*me)
565 Real staff_space = Staff_symbol_referencer::staff_space ((Grob*)me);
567 SCM details = me->get_grob_property ("details");
568 SCM h_inf_scm = me->get_grob_property ("height-limit");
569 SCM r_0_scm = me->get_grob_property ("ratio");
571 Real r_0 = gh_scm2double (r_0_scm);
572 Real h_inf = staff_space * gh_scm2double (h_inf_scm);
574 Slur_bezier_bow bb (get_encompass_offsets (me),
575 Directional_element_interface::get (me),
579 if (bb.fit_factor () > 1.0)
581 Real length = bb.curve_.control_[3][X_AXIS];
582 Real default_height = slur_height (length, h_inf, r_0);
584 SCM ssb = me->get_grob_property ("beautiful");
586 if (gh_number_p (ssb))
587 sb = gh_scm2double (ssb);
589 bb.minimise_enclosed_area (sb, details);
590 SCM sbf = scm_assq (ly_symbol2scm ("force-blowfit"), details);
592 if (gh_pair_p (sbf) && gh_number_p (ly_cdr (sbf)))
593 bff = gh_scm2double (ly_cdr (sbf));
595 bb.curve_.control_[1][Y_AXIS] *= bff;
596 bb.curve_.control_[2][Y_AXIS] *= bff;
600 Real beautiful = length * default_height * sb;
601 Real area = bb.get_enclosed_area ();
604 Slurs that fit beautifully are not ugly
606 if (area > beautiful)
607 de_uglyfy (me, &bb, default_height);
610 Bezier b = bb.get_bezier ();
613 SCM controls = SCM_EOL;
616 controls = gh_cons (ly_offset2scm (b.control_[i]), controls);
619 All these null control-points, where do they all come from?
621 if (i && b.control_[i][X_AXIS] == 0)
628 me->set_grob_property ("control-points", controls);
632 Slur::get_curve (Grob*me)
637 SCM attach = me->get_grob_property ("attachment");
638 if (!gh_pair_p (attach))
639 attach = set_extremities(me);
642 if (!Directional_element_interface::get (me)
643 || ! gh_symbol_p (index_get_cell (attach, LEFT))
644 || ! gh_symbol_p (index_get_cell (attach, RIGHT)))
645 set_extremities (me);
647 if (!gh_pair_p (me->get_grob_property ("control-points")))
648 set_control_points (me);
650 // set_control_points may suicide
651 if (!scm_ilength (me->get_grob_property ("note-columns")))
654 for (SCM s= me->get_grob_property ("control-points"); s != SCM_EOL; s = ly_cdr (s))
656 b.control_[i] = ly_scm2offset (ly_car (s));
660 Array<Offset> enc (get_encompass_offsets (me));
661 Direction dir = Directional_element_interface::get (me);
663 Real x1 = enc[0][X_AXIS];
664 Real x2 = enc.top ()[X_AXIS];
667 for (int i=1; i < enc.size ()-1; i++)
669 Real x = enc[i][X_AXIS];
672 Real y = b.get_other_coordinate (X_AXIS, x);
673 off = off >? dir * (enc[i][Y_AXIS] - y);
676 b.translate (Offset (0, dir * off));
683 ADD_INTERFACE (Slur,"slur-interface",
685 "attachment attachment-offset beautiful control-points dashed details de-uglify-parameters direction extremity-rules extremity-offset-alist height-limit ratio slope-limit thickness y-free");