2 tie.cc -- implement Tie
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@xs4all.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;
280 if (conf->position_ == dot_pos)
282 conf->position_ += conf->dir_;
283 in_space = !in_space;
288 Real y = staff_space * 0.5 * conf->position_;
289 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
290 conf->attachment_x_.widen (-gap);
291 Bezier b = slur_shape (conf->attachment_x_.length(),
292 details.height_limit_,
294 Offset middle = b.curve_point (0.5);
295 Offset edge = b.curve_point (0.0);
296 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
298 (dy < 0.6 * staff_space);
305 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
308 Stencil flag = Stem::get_translated_flag (left_stem);
309 Real y = conf->position_ * staff_space * 0.5;
310 if (flag.extent (Y_AXIS).contains (y))
312 conf->position_ += conf->dir_;
313 in_space = !in_space;
317 if (in_space != fits_in_space)
321 conf->position_ += conf->dir_;
326 conf->position_ += conf->dir_;
334 Real y = staff_space * 0.5 * conf->position_;
335 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
336 conf->attachment_x_.widen (-gap);
338 Bezier b = slur_shape (conf->attachment_x_.length(),
339 details.height_limit_,
341 Offset middle = b.curve_point (0.5);
342 Offset edge = b.curve_point (0.0);
343 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
345 (dy < 0.6 * staff_space);
351 Putting larger in-space ties next to the notes forces
352 the edges to be opposite (Y-wise) to the tie direction.
354 if (conf->position_ == conf->head_position_
356 && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
357 && dy > 0.3 * staff_space
360 conf->position_ += 2 * conf->dir_;
365 && abs (conf->position_ - conf->head_position_) <= 1)
367 int amount = conf->dir_;
368 if (sign (conf->position_) != conf->dir_
369 || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
372 conf->position_ += amount;
377 if ((sign (conf->position_) != conf->dir_
378 || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
380 ((abs (conf->position_ - conf->head_position_) <= 1
381 && fabs (dy) < 0.45 * staff_space)
382 || fabs (dy) < 0.6 * staff_space))
385 vertically center in space.
387 conf->center_tie_vertically (details);
392 conf->dir_ * staff_space * (- 0.3);
397 Real where = 0.5 * conf->dir_;
399 Real rounding_dy = (where - middle[Y_AXIS]);
400 conf->delta_y_ = rounding_dy;
402 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
403 + conf->position_ * staff_space * 0.5
405 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
407 if (Staff_symbol_referencer::staff_radius (me) > abs (conf->head_position_) / 2)
408 conf->position_ += 2 * conf->dir_;
410 conf->position_ += conf->dir_;
417 Real half_space = 0.5 * staff_space;
418 Real y = conf->position_ * half_space;
420 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
421 conf->attachment_x_.widen (-gap);
427 Tie::set_default_control_points (Grob *me_grob)
429 Spanner *me = dynamic_cast<Spanner*> (me_grob);
431 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
432 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
434 Tie_configuration conf;
435 int tie_position = (int) Tie::get_position (me);
436 conf.position_ = tie_position;
440 get_configuration (me, common, &conf, 0, details);
441 set_control_points (me, common, conf, details);
445 Tie::set_control_points (Grob *me,
447 Tie_configuration const &conf,
448 Tie_details const &details
451 Bezier b = conf.get_bezier (details);
452 b.scale (1, conf.dir_);
453 b.translate (Offset (conf.attachment_x_[LEFT]
454 - me->relative_coordinate (common, X_AXIS),
455 0.5 * conf.position_ * details.staff_space_
459 SCM controls = SCM_EOL;
460 for (int i = 4; i--;)
462 if (!b.control_[i].is_sane ())
463 programming_error ("Insane offset");
464 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
466 me->set_property ("control-points", controls);
469 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
471 Tie::calc_control_points (SCM smob)
473 Grob *me = unsmob_grob (smob);
475 // trigger Tie-column
476 (void) get_grob_direction (me);
478 Grob *yparent = me->get_parent (Y_AXIS);
479 if (Tie_column::has_interface (yparent)
480 && unsmob_grob_array (yparent->get_object ("ties"))
481 && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
483 /* trigger positioning. */
484 (void) yparent->get_property ("positioning-done");
487 if (!scm_is_pair (me->get_property ("control-points")))
489 set_default_control_points (me);
492 return SCM_UNSPECIFIED;
496 MAKE_SCHEME_CALLBACK (Tie, print, 1);
498 Tie::print (SCM smob)
500 Grob *me = unsmob_grob (smob);
502 SCM cp = me->get_property ("control-points");
504 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
505 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
506 Real thick = base_thick * staff_thick;
510 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
512 b.control_[i] = ly_scm2offset (scm_car (s));
518 SCM p = me->get_property ("dash-period");
519 SCM f = me->get_property ("dash-fraction");
520 if (scm_is_number (p) && scm_is_number (f))
521 a = Lookup::dashed_slur (b,
523 robust_scm2double (p, 1.0),
524 robust_scm2double (f, 0));
527 get_grob_direction (me) * staff_thick,
530 return a.smobbed_copy ();
536 "A tie connecting two noteheads. \n\n"