2 tie.cc -- implement Tie
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "paper-def.hh"
15 #include "rhythmic-head.hh"
17 #include "paper-column.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "directional-element-interface.hh"
21 #include "molecule.hh"
22 #include "bezier-bow.hh"
24 #include "note-head.hh"
27 tie: Connect two noteheads.
31 c4 ~ \clef bass ; c4 or
35 do we have non-horizontal ties then?
40 Tie::set_head (Grob*me,Direction d, Item * head_l)
42 assert (!head (me,d));
43 index_set_cell (me->get_grob_property ("heads"), d, head_l->self_scm ());
45 dynamic_cast<Spanner*> (me)->set_bound (d, head_l);
46 me->add_dependency (head_l);
50 Tie::set_interface (Grob*me)
52 me->set_grob_property ("heads", gh_cons (SCM_EOL, SCM_EOL));
57 Tie::head (Grob*me, Direction d)
59 SCM c = me->get_grob_property ("heads");
60 c = index_cell (c, d);
62 return unsmob_grob (c);
66 Tie::position_f (Grob*me)
68 Direction d = head (me,LEFT) ? LEFT:RIGHT;
69 return Staff_symbol_referencer::position_f (head (me,d));
74 Default: Put the tie oppositie of the stem [Wanske p231]
76 In case of chords: Tie_column takes over
78 The direction of the Tie is more complicated (See [Ross] p136 and
82 Tie::get_default_dir (Grob*me)
84 Item * sl = head (me,LEFT) ? Rhythmic_head::stem_l (head (me,LEFT)) :0;
85 Item * sr = head (me,RIGHT) ? Rhythmic_head::stem_l (head (me,RIGHT)) :0;
89 if (Directional_element_interface::get (sl) == UP
90 && Directional_element_interface::get (sr) == UP)
95 Item *s = sl ? sl : sr;
96 return - Directional_element_interface::get (s);
105 Tie::get_control_points (SCM smob)
107 Spanner*me = unsmob_spanner (smob);
108 Direction headdir = CENTER;
111 else if (head (me,RIGHT))
115 programming_error ("Tie without heads.");
117 return SCM_UNSPECIFIED;
120 if (!Directional_element_interface::get (me))
121 Directional_element_interface::set (me, Tie::get_default_dir (me));
123 Real staff_space = Staff_symbol_referencer::staff_space (me);
125 Real x_gap_f = gh_scm2double (me->get_grob_property ("x-gap"));
127 Grob* l = me->get_bound (LEFT);
128 Grob* r = me->get_bound (RIGHT);
130 Grob* commonx = me->common_refpoint (l, X_AXIS);
131 commonx = me->common_refpoint (r, X_AXIS);
136 this is a kludge: the tie has to be long enough to be
137 visible, but should not go through key sigs.
143 if (Note_head::has_interface (l))
144 left_x = l->extent (l, X_AXIS)[RIGHT] + x_gap_f;
146 left_x = l->extent (l, X_AXIS).linear_combination (lambda);
150 if (Note_head::has_interface (l) && Note_head::has_interface (r))
153 + r->extent (commonx,X_AXIS)[LEFT]
154 - l->extent (commonx, X_AXIS)[RIGHT]
159 if (Note_head::has_interface (l))
160 width = r->relative_coordinate (commonx, X_AXIS)
161 - l->extent (commonx, X_AXIS)[RIGHT]
165 - l->extent (commonx, X_AXIS).linear_combination (lambda)
166 + r->extent (commonx, X_AXIS)[LEFT]
170 Direction dir = Directional_element_interface::get (me);
172 SCM details = me->get_grob_property ("details");
174 SCM lim // groetjes aan de chirurgendochter.
175 = scm_assq (ly_symbol2scm ("height-limit"),details);
177 Real h_inf = gh_scm2double (ly_cdr (lim)) * staff_space;
178 Real r_0 = gh_scm2double (ly_cdr (scm_assq (ly_symbol2scm ("ratio"),details)));
180 Bezier b = slur_shape (width, h_inf, r_0);
183 I think this better, particularly for small ties. It always allows the user to move ties if
184 they seem in the wrong place
186 TODO: what if 2 heads have different size.
190 Real ypos = Tie::position_f (me) * staff_space/2
191 + dir * gh_scm2double (me->get_grob_property ("y-offset"));;
194 Make sure we don't start on a dots
196 if (Note_head::has_interface (l) && Rhythmic_head::dots_l (l))
198 Grob* dots = Rhythmic_head::dots_l(l);
199 if(fabs (staff_space * Staff_symbol_referencer::position_f (dots) /2
208 todo: prevent ending / staffline collision.
210 todo: tie / stem collision
213 b = slur_shape (width,h_inf, r_0);
215 b.translate (Offset (left_x, ypos));
219 Avoid colliding of the horizontal part with stafflines.
222 TODO: redo this, heuristic is half-baken, and ties often look ugly
225 TODO: doesn't work when on staff with even number of lines.
227 Array<Real> horizontal (b.solve_derivative (Offset (1,0)));
228 if (horizontal.size ())
231 ugh. Doesnt work for non-horizontal curves.
233 Real y = b.curve_point (horizontal[0])[Y_AXIS];
235 Real ry = rint (y/staff_space) * staff_space;
239 Real clear = staff_space * gh_scm2double (me->get_grob_property ("staffline-clearance"));
241 if (fabs (y) <= Staff_symbol_referencer::staff_radius (me)
242 && fabs (diff) < clear)
244 Real y1 = ry + clear;
245 Real y2 = ry - clear;
247 newy = (fabs (y1 - y) < fabs (y2 - y)) ? y1 : y2;
249 // newy = ry - 0.5 * staff_space * sign (diff) ;
252 we don't want horizontal ties
254 if (fabs (newy - b.control_[0][Y_AXIS]) < 1e-2)
256 newy = newy + dir * staff_space;
260 Real y0 = b.control_ [0][Y_AXIS];
261 b.control_[2][Y_AXIS] =
262 b.control_[1][Y_AXIS] =
263 (b.control_[1][Y_AXIS] - y0) * ((newy - y0) / (y - y0)) + y0;
266 programming_error ("Tie is nowhere horizontal");
270 SCM controls = SCM_EOL;
272 controls = gh_cons (ly_offset2scm (b.control_[i]), controls);
277 MAKE_SCHEME_CALLBACK (Tie,brew_molecule,1);
279 Tie::brew_molecule (SCM smob)
281 Grob*me = unsmob_grob (smob);
283 SCM cp = me->get_grob_property ("control-points");
286 cp = get_control_points (smob);
287 me->set_grob_property ("control-points", cp);
291 gh_scm2double (me->get_grob_property ("thickness"))
292 * me->paper_l ()->get_var ("linethickness");
296 for (SCM s= cp; s != SCM_EOL; s = ly_cdr (s))
298 b.control_[i] = ly_scm2offset (ly_car (s));
302 Molecule a = Lookup::slur (b, Directional_element_interface::get (me) * thick, thick);
304 return a.smobbed_copy ();
309 ADD_INTERFACE (Tie,"tie-interface",
310 "A tie connecting two noteheads.
311 direction = Forced direction for all ties",
312 "y-offset staffline-clearance control-points heads details thickness x-gap direction minimum-length");