2 tie.cc -- implement Tie
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
15 #include "output-def.hh"
16 #include "rhythmic-head.hh"
18 #include "paper-column.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "directional-element-interface.hh"
24 #include "note-head.hh"
25 #include "tie-column.hh"
28 tie: Connect two noteheads.
32 c4 ~ \clef bass ; c4 or
36 do we have non-horizontal ties then?
40 Tie::set_head (Grob *me, Direction d, Grob *h)
42 dynamic_cast<Spanner *> (me)->set_bound (d, h);
43 me->add_dependency (h);
47 Tie::head (Grob *me, Direction d)
49 Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
50 if (Note_head::has_interface (it))
57 Tie::get_column_rank (Grob *me, Direction d)
59 Spanner *span = dynamic_cast<Spanner *> (me);
60 Grob *h = head (me, d);
62 h = span->get_bound (d);
64 Grob *col = dynamic_cast<Item *> (h)->get_column ();
65 return Paper_column::get_rank (col);
69 Tie::get_position (Grob *me)
71 Direction d = head (me, LEFT) ? LEFT : RIGHT;
72 return Staff_symbol_referencer::get_position (head (me, d));
76 Default: Put the tie oppositie of the stem [Wanske p231]
78 In case of chords: Tie_column takes over
80 The direction of the Tie is more complicated (See [Ross] p136 and
83 (what about linebreaks? )
86 Tie::get_default_dir (Grob *me)
88 Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
89 Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
92 if (get_grob_direction (sl) == UP
93 && get_grob_direction (sr) == UP)
98 Item *s = sl ? sl : sr;
99 return -get_grob_direction (s);
106 Tie::set_direction (Grob *me)
108 if (!get_grob_direction (me))
110 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
111 Tie_column::set_directions (me->get_parent (Y_AXIS));
113 set_grob_direction (me, Tie::get_default_dir (me));
119 Tie::get_configuration (Grob *me_grob, Grob **common,
120 Tie_configuration *conf)
122 Spanner *me = dynamic_cast<Spanner*> (me_grob);
123 if (!head (me, LEFT) && !head (me, RIGHT))
125 programming_error ("tie without heads");
130 Direction dir = CENTER;
132 int tie_position = (int) Tie::get_position (me);
133 int staff_position = conf->position_;
141 dir = get_grob_direction (me);
143 dir = get_default_dir (me);
146 Real staff_space = Staff_symbol_referencer::staff_space (me);
148 bool in_between = true;
149 Interval attachments = conf->attachment_x_;
150 if (attachments.is_empty())
153 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
157 = robust_relative_extent (me->get_bound (d),
162 while (flip (&d) != LEFT);
164 if (attachments.length () < 0.6 * staff_space)
167 Let short ties start over note heads, instead of between.
169 Drul_array<bool> allow (true, true);
173 if (Note_head::has_interface (me->get_bound (d)))
175 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
176 if (get_grob_direction (stem) == dir
180 } while (flip (&d) != LEFT);
182 if (allow[LEFT] && allow[RIGHT])
184 staff_position += dir;
187 if (Note_head::has_interface (me->get_bound (d)))
190 = robust_relative_extent (me->get_bound (d),
191 common[X_AXIS], X_AXIS);
193 attachments[d] = extent.linear_combination (- 0.5 * d);
197 while (flip (&d) != LEFT);
201 SCM details = me->get_property ("details");
204 = scm_assq (ly_symbol2scm ("height-limit"), details);
206 Real h_inf = robust_scm2double (scm_cdr (limit), 0.75) * staff_space;
207 Real r_0 = robust_scm2double (scm_cdr (scm_assq (ly_symbol2scm ("ratio"), details)),
210 Bezier b = slur_shape (attachments.length(),
214 Offset middle = b.curve_point (0.5);
215 Offset edge = b.curve_point (0.0);
217 staff_position = int (rint (staff_position));
219 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
220 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) staff_position));
222 (dy < 0.6 * staff_space);
227 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
228 if (left_dot && in_space)
230 if (staff_position == Staff_symbol_referencer::get_position (left_dot))
232 staff_position += dir;
240 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
243 Stencil flag = Stem::get_translated_flag (left_stem);
244 Real y = staff_position * staff_space * 0.5;
245 if (flag.extent (Y_AXIS).contains (y))
247 staff_position += dir;
248 in_space = !in_space;
252 if (in_space != fits_in_space)
256 staff_position += dir;
261 staff_position += dir;
267 Putting larger in-space ties next to the notes forces
268 the edges to be opposite (Y-wise) to the tie direction.
270 if (staff_position == tie_position
272 && dy > 0.3 * staff_space)
274 staff_position += 2 * dir;
279 && fabs (staff_position - tie_position) <= 1)
280 staff_position += 2*dir;
285 if (fabs (dy) < 0.4 * staff_space)
288 vertically center in space.
290 Offset middle = b.curve_point (0.5);
291 Offset edge = b.curve_point (0.0);
293 Real center = (edge[Y_AXIS] + middle[Y_AXIS])/2.0;
295 conf->delta_y_ = - center;
300 dir * staff_space * (- 0.3);
305 Real where = 0.5 * dir;
307 Real rounding_dy = (where - middle[Y_AXIS]);
308 conf->delta_y_ = rounding_dy;
310 if (dir * b.curve_point (0.0)[Y_AXIS] <
311 dir * tie_position * 0.5 * staff_space)
312 conf->delta_y_ += staff_space * dir;
316 conf->position_ = staff_position;
317 conf->attachment_x_ = attachments;
322 Tie::set_default_control_points (Grob *me_grob)
324 Spanner *me = dynamic_cast<Spanner*> (me_grob);
325 Grob *common[NO_AXES] = {
328 for (int a = X_AXIS; a < NO_AXES; a++)
331 common[ax] = me->get_bound (LEFT)->common_refpoint (me, ax);
332 common[ax] = me->get_bound (RIGHT)->common_refpoint (common[a], ax);
335 Tie_configuration conf;
336 if (!get_grob_direction (me))
337 set_grob_direction (me, get_default_dir (me));
339 int tie_position = (int) Tie::get_position (me);
340 conf.position_ = tie_position;
343 get_configuration (me, common, &conf);
344 set_control_points (me, common, conf);
348 Tie::set_control_points (Grob *me,
350 Tie_configuration const &conf)
352 SCM details = me->get_property ("details");
354 = scm_assq (ly_symbol2scm ("height-limit"), details);
356 Real staff_space = Staff_symbol_referencer::staff_space (me);
357 Real h_inf = robust_scm2double (scm_cdr (limit), 0.75) * staff_space;
358 Real r_0 = robust_scm2double (scm_cdr (scm_assq (ly_symbol2scm ("ratio"),
362 Bezier b = slur_shape (conf.attachment_x_.length(),
364 b.scale (1, conf.dir_);
365 b.translate (Offset (conf.attachment_x_[LEFT]
366 - me->relative_coordinate (common[X_AXIS], X_AXIS),
367 0.5 * conf.position_ * staff_space
371 SCM controls = SCM_EOL;
372 for (int i = 4; i--;)
373 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
375 me->set_property ("control-points", controls);
380 MAKE_SCHEME_CALLBACK (Tie, print, 1);
382 Tie::print (SCM smob)
384 Grob *me = unsmob_grob (smob);
386 if (CENTER == get_grob_direction (me))
389 SCM cp = me->get_property ("control-points");
390 if (!scm_is_pair (cp))
392 set_default_control_points (me);
393 cp = me->get_property ("control-points");
396 if (!scm_is_pair (cp))
397 return Stencil ().smobbed_copy ();
399 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
400 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
401 Real thick = base_thick * staff_thick;
405 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
407 b.control_[i] = ly_scm2offset (scm_car (s));
413 SCM p = me->get_property ("dash-period");
414 SCM f = me->get_property ("dash-fraction");
415 if (scm_is_number (p) && scm_is_number (f))
416 a = Lookup::dashed_slur (b,
418 robust_scm2double (p, 1.0),
419 robust_scm2double (f, 0));
422 get_grob_direction (me) * staff_thick,
425 return a.smobbed_copy ();
431 "A tie connecting two noteheads.\n",
442 Tie_configuration::compare (Tie_configuration const &a,
443 Tie_configuration const &b)
445 if (a.position_ - b.position_)
446 return sign (a.position_ - b.position_);
447 return sign (a.dir_ - b.dir_);
451 Tie_configuration::Tie_configuration ()