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>
14 #include "output-def.hh"
15 #include "rhythmic-head.hh"
17 #include "paper-column.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "directional-element-interface.hh"
23 #include "note-head.hh"
24 #include "tie-column.hh"
27 Tie::compare (Grob *const &s1,
30 return sign (Tie::get_position (s1) - Tie::get_position (s2));
34 Tie::set_head (Grob *me, Direction d, Grob *h)
36 dynamic_cast<Spanner *> (me)->set_bound (d, h);
37 me->add_dependency (h);
41 Tie::head (Grob *me, Direction d)
43 Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
44 if (Note_head::has_interface (it))
51 Tie::get_column_rank (Grob *me, Direction d)
53 Spanner *span = dynamic_cast<Spanner *> (me);
54 Grob *h = head (me, d);
56 h = span->get_bound (d);
58 Grob *col = dynamic_cast<Item *> (h)->get_column ();
59 return Paper_column::get_rank (col);
63 Tie::get_position (Grob *me)
65 Direction d = head (me, LEFT) ? LEFT : RIGHT;
66 return (int) Staff_symbol_referencer::get_position (head (me, d));
70 Default: Put the tie oppositie of the stem [Wanske p231]
72 In case of chords: Tie_column takes over
74 The direction of the Tie is more complicated (See [Ross] p136 and
77 (what about linebreaks? )
80 Tie::get_default_dir (Grob *me)
82 Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
83 Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
86 if (get_grob_direction (sl) == UP
87 && get_grob_direction (sr) == UP)
92 Item *s = sl ? sl : sr;
93 return -get_grob_direction (s);
100 Tie::set_direction (Grob *me)
102 if (!get_grob_direction (me))
104 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
105 Tie_column::set_directions (me->get_parent (Y_AXIS));
107 set_grob_direction (me, Tie::get_default_dir (me));
112 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
115 Tie_details const &details
118 Real staff_space = Staff_symbol_referencer::staff_space (me);
119 Direction dir = get_grob_direction (me);
120 Interval attachments;
125 = robust_relative_extent (me->get_bound (d),
130 while (flip (&d) != LEFT);
132 if (attachments.length () < details.between_length_limit_ * staff_space)
135 Let short ties start over note heads, instead of between.
137 Drul_array<bool> allow (true, true);
141 if (Note_head::has_interface (me->get_bound (d)))
143 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
144 if (get_grob_direction (stem) == dir
148 } while (flip (&d) != LEFT);
150 if (allow[LEFT] && allow[RIGHT])
152 *staff_position += dir;
155 if (Note_head::has_interface (me->get_bound (d)))
158 = robust_relative_extent (me->get_bound (d),
161 attachments[d] = extent.linear_combination (- 0.5 * d);
165 while (flip (&d) != LEFT);
173 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
176 Interval attachments;
180 attachments[d] = skyline_height (skylines[d], y, -d);
182 while (flip (&d) != LEFT);
188 Tie::get_configuration (Grob *me_grob, Grob *common,
189 Tie_configuration *conf,
190 Drul_array< Array < Skyline_entry > > const *skylines,
191 Tie_details const &details
194 Spanner *me = dynamic_cast<Spanner*> (me_grob);
195 if (!head (me, LEFT) && !head (me, RIGHT))
197 programming_error ("tie without heads");
203 UGH. Don't mirror Tie_configuration.
205 conf->head_position_ = (int) Tie::get_position (me);
207 conf->dir_ = get_grob_direction (me);
209 conf->dir_ = get_default_dir (me);
211 Real staff_space = details.staff_space_;
212 bool in_between = true;
213 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
215 if (conf->attachment_x_.is_empty())
218 conf->attachment_x_ = get_default_attachments (me, common, gap,
220 &in_between, details);
223 Real y = staff_space * 0.5 * conf->position_;
224 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
225 conf->attachment_x_.widen (-gap);
229 Bezier b = slur_shape (conf->attachment_x_.length(),
230 details.height_limit_,
232 b.scale (1, conf->dir_);
234 Offset middle = b.curve_point (0.5);
235 Offset edge = b.curve_point (0.0);
237 conf->position_ = int (rint (conf->position_));
239 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
240 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
242 (dy < 0.6 * staff_space);
247 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
248 int dot_pos = left_dot
249 ? int (Staff_symbol_referencer::get_position (left_dot))
252 && (conf->position_ == dot_pos
253 || conf->position_ + conf->dir_ == dot_pos))
255 conf->position_ += conf->dir_;
256 in_space = !in_space;
260 Real y = staff_space * 0.5 * conf->position_;
261 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
262 conf->attachment_x_.widen (-gap);
263 Bezier b = slur_shape (conf->attachment_x_.length(),
264 details.height_limit_,
266 Offset middle = b.curve_point (0.5);
267 Offset edge = b.curve_point (0.0);
268 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
270 (dy < 0.6 * staff_space);
277 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
280 Stencil flag = Stem::get_translated_flag (left_stem);
281 Real y = conf->position_ * staff_space * 0.5;
282 if (flag.extent (Y_AXIS).contains (y))
284 conf->position_ += conf->dir_;
285 in_space = !in_space;
289 if (in_space != fits_in_space)
293 conf->position_ += conf->dir_;
298 conf->position_ += conf->dir_;
306 Real y = staff_space * 0.5 * conf->position_;
307 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
308 conf->attachment_x_.widen (-gap);
310 Bezier b = slur_shape (conf->attachment_x_.length(),
311 details.height_limit_,
313 Offset middle = b.curve_point (0.5);
314 Offset edge = b.curve_point (0.0);
315 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
317 (dy < 0.6 * staff_space);
323 Putting larger in-space ties next to the notes forces
324 the edges to be opposite (Y-wise) to the tie direction.
326 if (conf->position_ == conf->head_position_
328 && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
329 && dy > 0.3 * staff_space
332 conf->position_ += 2 * conf->dir_;
337 && abs (conf->position_ - conf->head_position_) <= 1)
338 conf->position_ += 2*conf->dir_;
343 if ((abs (conf->position_ - conf->head_position_) <= 1
344 && fabs (dy) < 0.45 * staff_space)
345 || fabs (dy) < 0.6 * staff_space)
348 vertically center in space.
350 conf->center_tie_vertically (details);
355 conf->dir_ * staff_space * (- 0.3);
360 Real where = 0.5 * conf->dir_;
362 Real rounding_dy = (where - middle[Y_AXIS]);
363 conf->delta_y_ = rounding_dy;
365 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
366 + conf->position_ * staff_space * 0.5
368 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
370 if (Staff_symbol_referencer::staff_radius (me) > abs (conf->head_position_) / 2)
371 conf->position_ += 2 * conf->dir_;
373 conf->position_ += conf->dir_;
380 Real half_space = 0.5 * staff_space;
381 Real y = conf->position_ * half_space;
383 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
384 conf->attachment_x_.widen (-gap);
390 Tie::set_default_control_points (Grob *me_grob)
392 Spanner *me = dynamic_cast<Spanner*> (me_grob);
394 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
395 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
397 Tie_configuration conf;
398 if (!get_grob_direction (me))
399 set_grob_direction (me, get_default_dir (me));
401 int tie_position = (int) Tie::get_position (me);
402 conf.position_ = tie_position;
406 get_configuration (me, common, &conf, 0, details);
407 set_control_points (me, common, conf, details);
411 Tie::set_control_points (Grob *me,
413 Tie_configuration const &conf,
414 Tie_details const &details
417 Bezier b = conf.get_bezier (details);
418 b.scale (1, conf.dir_);
419 b.translate (Offset (conf.attachment_x_[LEFT]
420 - me->relative_coordinate (common, X_AXIS),
421 0.5 * conf.position_ * details.staff_space_
425 SCM controls = SCM_EOL;
426 for (int i = 4; i--;)
428 if (!b.control_[i].is_sane ())
429 programming_error ("Insane offset");
430 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
432 me->set_property ("control-points", controls);
437 MAKE_SCHEME_CALLBACK (Tie, print, 1);
439 Tie::print (SCM smob)
441 Grob *me = unsmob_grob (smob);
443 if (CENTER == get_grob_direction (me))
446 if (!get_grob_direction (me))
447 me->programming_error ("Tie direction not set.");
449 SCM cp = me->get_property ("control-points");
450 if (!scm_is_pair (cp))
453 UGH. dependency tracking!
455 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
457 Tie_column::set_directions (me->get_parent (Y_AXIS));
460 cp = me->get_property ("control-points");
463 if (!scm_is_pair (cp))
465 set_default_control_points (me);
466 cp = me->get_property ("control-points");
469 if (!scm_is_pair (cp))
470 return Stencil ().smobbed_copy ();
472 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
473 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
474 Real thick = base_thick * staff_thick;
478 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
480 b.control_[i] = ly_scm2offset (scm_car (s));
486 SCM p = me->get_property ("dash-period");
487 SCM f = me->get_property ("dash-fraction");
488 if (scm_is_number (p) && scm_is_number (f))
489 a = Lookup::dashed_slur (b,
491 robust_scm2double (p, 1.0),
492 robust_scm2double (f, 0));
495 get_grob_direction (me) * staff_thick,
498 return a.smobbed_copy ();
504 "A tie connecting two noteheads. \n\n"