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>
12 #include "output-def.hh"
13 #include "rhythmic-head.hh"
15 #include "paper-column.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "directional-element-interface.hh"
21 #include "note-head.hh"
22 #include "tie-column.hh"
23 #include "grob-array.hh"
26 Tie::compare (Grob *const &s1,
29 return sign (Tie::get_position (s1) - Tie::get_position (s2));
33 Tie::set_head (Grob *me, Direction d, Grob *h)
35 dynamic_cast<Spanner *> (me)->set_bound (d, h);
39 Tie::head (Grob *me, Direction d)
41 Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
42 if (Note_head::has_interface (it))
49 Tie::get_column_rank (Grob *me, Direction d)
51 Spanner *span = dynamic_cast<Spanner *> (me);
52 Grob *h = head (me, d);
54 h = span->get_bound (d);
56 Grob *col = dynamic_cast<Item *> (h)->get_column ();
57 return Paper_column::get_rank (col);
61 Tie::get_position (Grob *me)
66 Grob *h = head (me, d);
68 return (int) Staff_symbol_referencer::get_position (h);
70 while (flip (&d) != LEFT);
74 TODO: this is theoretically possible for ties across more than 2
75 systems.. We should look at the first broken copy.
78 programming_error ("Tie without heads. Suicide");
84 Default: Put the tie oppositie of the stem [Wanske p231]
86 In case of chords: Tie_column takes over
88 The direction of the Tie is more complicated (See [Ross] p136 and
91 (what about linebreaks? )
94 Tie::get_default_dir (Grob *me)
96 Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
97 Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
100 if (get_grob_direction (sl) == UP
101 && get_grob_direction (sr) == UP)
106 Item *s = sl ? sl : sr;
107 return -get_grob_direction (s);
114 MAKE_SCHEME_CALLBACK(Tie, calc_direction, 1);
116 Tie::calc_direction (SCM smob)
118 Grob *me = unsmob_grob (smob);
119 Grob *yparent = me->get_parent (Y_AXIS);
120 if (Tie_column::has_interface (yparent)
121 && unsmob_grob_array (yparent->get_object ("ties"))
122 && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
124 /* trigger positioning. */
125 (void) yparent->get_property ("positioning-done");
128 set_grob_direction (me, Tie::get_default_dir (me));
130 return SCM_UNSPECIFIED;
134 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
137 Tie_details const &details
140 Real staff_space = Staff_symbol_referencer::staff_space (me);
141 Direction dir = get_grob_direction (me);
142 Interval attachments;
147 = robust_relative_extent (me->get_bound (d),
152 while (flip (&d) != LEFT);
154 if (attachments.length () < details.between_length_limit_ * staff_space)
157 Let short ties start over note heads, instead of between.
159 Drul_array<bool> allow (true, true);
163 if (Note_head::has_interface (me->get_bound (d)))
165 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
166 if (get_grob_direction (stem) == dir
170 } while (flip (&d) != LEFT);
172 if (allow[LEFT] && allow[RIGHT])
174 *staff_position += dir;
177 if (Note_head::has_interface (me->get_bound (d)))
180 = robust_relative_extent (me->get_bound (d),
183 attachments[d] = extent.linear_combination (- 0.5 * d);
187 while (flip (&d) != LEFT);
195 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
198 Interval attachments;
202 attachments[d] = skyline_height (skylines[d], y, -d);
204 while (flip (&d) != LEFT);
210 Tie::get_configuration (Grob *me_grob, Grob *common,
211 Tie_configuration *conf,
212 Drul_array< Array < Skyline_entry > > const *skylines,
213 Tie_details const &details
216 Spanner *me = dynamic_cast<Spanner*> (me_grob);
217 if (!head (me, LEFT) && !head (me, RIGHT))
219 programming_error ("tie without heads");
225 UGH. Don't mirror Tie_configuration.
227 conf->head_position_ = (int) Tie::get_position (me);
229 conf->dir_ = get_grob_direction (me);
231 conf->dir_ = get_default_dir (me);
233 Real staff_space = details.staff_space_;
234 bool in_between = true;
235 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
237 if (conf->attachment_x_.is_empty())
240 conf->attachment_x_ = get_default_attachments (me, common, gap,
242 &in_between, details);
245 Real y = staff_space * 0.5 * conf->position_;
246 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
247 conf->attachment_x_.widen (-gap);
251 Bezier b = slur_shape (conf->attachment_x_.length(),
252 details.height_limit_,
254 b.scale (1, conf->dir_);
256 Offset middle = b.curve_point (0.5);
257 Offset edge = b.curve_point (0.0);
259 conf->position_ = int (rint (conf->position_));
261 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
262 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
264 (dy < 0.6 * staff_space);
269 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
270 int dot_pos = left_dot
271 ? int (Staff_symbol_referencer::get_position (left_dot))
274 && (conf->position_ == dot_pos
275 || conf->position_ + conf->dir_ == dot_pos))
277 conf->position_ += conf->dir_;
278 in_space = !in_space;
282 Real y = staff_space * 0.5 * conf->position_;
283 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
284 conf->attachment_x_.widen (-gap);
285 Bezier b = slur_shape (conf->attachment_x_.length(),
286 details.height_limit_,
288 Offset middle = b.curve_point (0.5);
289 Offset edge = b.curve_point (0.0);
290 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
292 (dy < 0.6 * staff_space);
299 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
302 Stencil flag = Stem::get_translated_flag (left_stem);
303 Real y = conf->position_ * staff_space * 0.5;
304 if (flag.extent (Y_AXIS).contains (y))
306 conf->position_ += conf->dir_;
307 in_space = !in_space;
311 if (in_space != fits_in_space)
315 conf->position_ += conf->dir_;
320 conf->position_ += conf->dir_;
328 Real y = staff_space * 0.5 * conf->position_;
329 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
330 conf->attachment_x_.widen (-gap);
332 Bezier b = slur_shape (conf->attachment_x_.length(),
333 details.height_limit_,
335 Offset middle = b.curve_point (0.5);
336 Offset edge = b.curve_point (0.0);
337 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
339 (dy < 0.6 * staff_space);
345 Putting larger in-space ties next to the notes forces
346 the edges to be opposite (Y-wise) to the tie direction.
348 if (conf->position_ == conf->head_position_
350 && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
351 && dy > 0.3 * staff_space
354 conf->position_ += 2 * conf->dir_;
359 && abs (conf->position_ - conf->head_position_) <= 1)
360 conf->position_ += 2*conf->dir_;
365 if ((abs (conf->position_ - conf->head_position_) <= 1
366 && fabs (dy) < 0.45 * staff_space)
367 || fabs (dy) < 0.6 * staff_space)
370 vertically center in space.
372 conf->center_tie_vertically (details);
377 conf->dir_ * staff_space * (- 0.3);
382 Real where = 0.5 * conf->dir_;
384 Real rounding_dy = (where - middle[Y_AXIS]);
385 conf->delta_y_ = rounding_dy;
387 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
388 + conf->position_ * staff_space * 0.5
390 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
392 if (Staff_symbol_referencer::staff_radius (me) > abs (conf->head_position_) / 2)
393 conf->position_ += 2 * conf->dir_;
395 conf->position_ += conf->dir_;
402 Real half_space = 0.5 * staff_space;
403 Real y = conf->position_ * half_space;
405 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
406 conf->attachment_x_.widen (-gap);
412 Tie::set_default_control_points (Grob *me_grob)
414 Spanner *me = dynamic_cast<Spanner*> (me_grob);
416 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
417 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
419 Tie_configuration conf;
420 int tie_position = (int) Tie::get_position (me);
421 conf.position_ = tie_position;
425 get_configuration (me, common, &conf, 0, details);
426 set_control_points (me, common, conf, details);
430 Tie::set_control_points (Grob *me,
432 Tie_configuration const &conf,
433 Tie_details const &details
436 Bezier b = conf.get_bezier (details);
437 b.scale (1, conf.dir_);
438 b.translate (Offset (conf.attachment_x_[LEFT]
439 - me->relative_coordinate (common, X_AXIS),
440 0.5 * conf.position_ * details.staff_space_
444 SCM controls = SCM_EOL;
445 for (int i = 4; i--;)
447 if (!b.control_[i].is_sane ())
448 programming_error ("Insane offset");
449 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
451 me->set_property ("control-points", controls);
454 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
456 Tie::calc_control_points (SCM smob)
458 Grob *me = unsmob_grob (smob);
460 // trigger Tie-column
461 (void) get_grob_direction (me);
463 if (!scm_is_pair (me->get_property ("control-points")))
465 set_default_control_points (me);
468 return SCM_UNSPECIFIED;
472 MAKE_SCHEME_CALLBACK (Tie, print, 1);
474 Tie::print (SCM smob)
476 Grob *me = unsmob_grob (smob);
478 SCM cp = me->get_property ("control-points");
480 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
481 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
482 Real thick = base_thick * staff_thick;
486 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
488 b.control_[i] = ly_scm2offset (scm_car (s));
494 SCM p = me->get_property ("dash-period");
495 SCM f = me->get_property ("dash-fraction");
496 if (scm_is_number (p) && scm_is_number (f))
497 a = Lookup::dashed_slur (b,
499 robust_scm2double (p, 1.0),
500 robust_scm2double (f, 0));
503 get_grob_direction (me) * staff_thick,
506 return a.smobbed_copy ();
512 "A tie connecting two noteheads. \n\n"