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 (int) 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));
118 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
123 Real staff_space = Staff_symbol_referencer::staff_space (me);
124 Direction dir = get_grob_direction (me);
125 Interval attachments;
130 = robust_relative_extent (me->get_bound (d),
135 while (flip (&d) != LEFT);
137 if (attachments.length () < 0.6 * staff_space)
140 Let short ties start over note heads, instead of between.
142 Drul_array<bool> allow (true, true);
146 if (Note_head::has_interface (me->get_bound (d)))
148 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
149 if (get_grob_direction (stem) == dir
153 } while (flip (&d) != LEFT);
155 if (allow[LEFT] && allow[RIGHT])
157 *staff_position += dir;
160 if (Note_head::has_interface (me->get_bound (d)))
163 = robust_relative_extent (me->get_bound (d),
166 attachments[d] = extent.linear_combination (- 0.5 * d);
170 while (flip (&d) != LEFT);
178 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
181 Interval attachments;
185 attachments[d] = skyline_height (skylines[d], y, -d);
187 while (flip (&d) != LEFT);
193 Tie::get_configuration (Grob *me_grob, Grob *common,
194 Tie_configuration *conf,
195 Drul_array< Array < Skyline_entry > > const *skylines,
196 Tie_details const &details
199 Spanner *me = dynamic_cast<Spanner*> (me_grob);
200 if (!head (me, LEFT) && !head (me, RIGHT))
202 programming_error ("tie without heads");
208 UGH. Don't mirror Tie_configuration.
210 conf->head_position_ = (int) Tie::get_position (me);
212 conf->dir_ = get_grob_direction (me);
214 conf->dir_ = get_default_dir (me);
217 Real staff_space = details.staff_space_;
218 bool in_between = true;
219 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
221 if (conf->attachment_x_.is_empty())
224 conf->attachment_x_ = get_default_attachments (me, common, gap,
229 Real y = staff_space * 0.5 * conf->position_;
230 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
231 conf->attachment_x_.widen (-gap);
235 Bezier b = slur_shape (conf->attachment_x_.length(),
236 details.height_limit_,
238 b.scale (1, conf->dir_);
240 Offset middle = b.curve_point (0.5);
241 Offset edge = b.curve_point (0.0);
243 conf->position_ = int (rint (conf->position_));
245 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
246 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
248 (dy < 0.6 * staff_space);
253 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
254 int dot_pos = left_dot
255 ? int (Staff_symbol_referencer::get_position (left_dot))
258 && (conf->position_ == dot_pos
259 || conf->position_ + conf->dir_ == dot_pos))
261 conf->position_ += conf->dir_;
262 in_space = !in_space;
266 Real y = staff_space * 0.5 * conf->position_;
267 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
268 conf->attachment_x_.widen (-gap);
269 Bezier b = slur_shape (conf->attachment_x_.length(),
270 details.height_limit_,
272 Offset middle = b.curve_point (0.5);
273 Offset edge = b.curve_point (0.0);
274 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
276 (dy < 0.6 * staff_space);
283 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
286 Stencil flag = Stem::get_translated_flag (left_stem);
287 Real y = conf->position_ * staff_space * 0.5;
288 if (flag.extent (Y_AXIS).contains (y))
290 conf->position_ += conf->dir_;
291 in_space = !in_space;
295 if (in_space != fits_in_space)
299 conf->position_ += conf->dir_;
304 conf->position_ += conf->dir_;
312 Real y = staff_space * 0.5 * conf->position_;
313 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
314 conf->attachment_x_.widen (-gap);
316 Bezier b = slur_shape (conf->attachment_x_.length(),
317 details.height_limit_,
319 Offset middle = b.curve_point (0.5);
320 Offset edge = b.curve_point (0.0);
321 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
323 (dy < 0.6 * staff_space);
329 Putting larger in-space ties next to the notes forces
330 the edges to be opposite (Y-wise) to the tie direction.
332 if (conf->position_ == conf->head_position_
334 && Staff_symbol_referencer::staff_radius (me) > fabs (conf->position_) / 2
335 && dy > 0.3 * staff_space)
337 conf->position_ += 2 * conf->dir_;
342 && fabs (conf->position_ - conf->head_position_) <= 1)
343 conf->position_ += 2*conf->dir_;
348 if ((fabs (conf->position_ - conf->head_position_) <= 1
349 && fabs (dy) < 0.45 * staff_space)
350 || fabs (dy) < 0.6 * staff_space)
353 vertically center in space.
355 conf->center_tie_vertically (details);
360 conf->dir_ * staff_space * (- 0.3);
365 Real where = 0.5 * conf->dir_;
367 Real rounding_dy = (where - middle[Y_AXIS]);
368 conf->delta_y_ = rounding_dy;
370 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
371 + conf->position_ * staff_space * 0.5
373 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
375 if (Staff_symbol_referencer::staff_radius (me) > fabs (conf->head_position_) / 2)
376 conf->position_ += 2 * conf->dir_;
378 conf->position_ += conf->dir_;
385 Real half_space = 0.5 * staff_space;
386 Real y = conf->position_ * half_space;
388 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
389 conf->attachment_x_.widen (-gap);
395 Tie::set_default_control_points (Grob *me_grob)
397 Spanner *me = dynamic_cast<Spanner*> (me_grob);
399 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
400 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
402 Tie_configuration conf;
403 if (!get_grob_direction (me))
404 set_grob_direction (me, get_default_dir (me));
406 int tie_position = (int) Tie::get_position (me);
407 conf.position_ = tie_position;
411 get_configuration (me, common, &conf, 0, details);
412 set_control_points (me, common, conf, details);
416 Tie::set_control_points (Grob *me,
418 Tie_configuration const &conf,
419 Tie_details const &details
422 Bezier b = conf.get_bezier (details);
423 b.scale (1, conf.dir_);
424 b.translate (Offset (conf.attachment_x_[LEFT]
425 - me->relative_coordinate (common, X_AXIS),
426 0.5 * conf.position_ * details.staff_space_
430 SCM controls = SCM_EOL;
431 for (int i = 4; i--;)
433 if (!b.control_[i].is_sane ())
434 programming_error ("Insane offset");
435 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
437 me->set_property ("control-points", controls);
442 MAKE_SCHEME_CALLBACK (Tie, print, 1);
444 Tie::print (SCM smob)
446 Grob *me = unsmob_grob (smob);
448 if (CENTER == get_grob_direction (me))
451 if (!get_grob_direction (me))
452 me->programming_error ("Tie direction not set.");
454 SCM cp = me->get_property ("control-points");
455 if (!scm_is_pair (cp))
458 UGH. dependency tracking!
460 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
462 Tie_column::set_directions (me->get_parent (Y_AXIS));
465 cp = me->get_property ("control-points");
468 if (!scm_is_pair (cp))
470 set_default_control_points (me);
471 cp = me->get_property ("control-points");
474 if (!scm_is_pair (cp))
475 return Stencil ().smobbed_copy ();
477 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
478 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
479 Real thick = base_thick * staff_thick;
483 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
485 b.control_[i] = ly_scm2offset (scm_car (s));
491 SCM p = me->get_property ("dash-period");
492 SCM f = me->get_property ("dash-fraction");
493 if (scm_is_number (p) && scm_is_number (f))
494 a = Lookup::dashed_slur (b,
496 robust_scm2double (p, 1.0),
497 robust_scm2double (f, 0));
500 get_grob_direction (me) * staff_thick,
503 return a.smobbed_copy ();
509 "A tie connecting two noteheads.\n",