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,
117 Real staff_space = Staff_symbol_referencer::staff_space (me);
118 Direction dir = get_grob_direction (me);
119 Interval attachments;
124 = robust_relative_extent (me->get_bound (d),
129 while (flip (&d) != LEFT);
131 if (attachments.length () < 0.6 * staff_space)
134 Let short ties start over note heads, instead of between.
136 Drul_array<bool> allow (true, true);
140 if (Note_head::has_interface (me->get_bound (d)))
142 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
143 if (get_grob_direction (stem) == dir
147 } while (flip (&d) != LEFT);
149 if (allow[LEFT] && allow[RIGHT])
151 *staff_position += dir;
154 if (Note_head::has_interface (me->get_bound (d)))
157 = robust_relative_extent (me->get_bound (d),
160 attachments[d] = extent.linear_combination (- 0.5 * d);
164 while (flip (&d) != LEFT);
172 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
175 Interval attachments;
179 attachments[d] = skyline_height (skylines[d], y, -d);
181 while (flip (&d) != LEFT);
187 Tie::get_configuration (Grob *me_grob, Grob *common,
188 Tie_configuration *conf,
189 Drul_array< Array < Skyline_entry > > const *skylines,
190 Tie_details const &details
193 Spanner *me = dynamic_cast<Spanner*> (me_grob);
194 if (!head (me, LEFT) && !head (me, RIGHT))
196 programming_error ("tie without heads");
202 UGH. Don't mirror Tie_configuration.
204 conf->head_position_ = (int) Tie::get_position (me);
206 conf->dir_ = get_grob_direction (me);
208 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,
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)
331 conf->position_ += 2 * conf->dir_;
336 && abs (conf->position_ - conf->head_position_) <= 1)
337 conf->position_ += 2*conf->dir_;
342 if ((abs (conf->position_ - conf->head_position_) <= 1
343 && fabs (dy) < 0.45 * staff_space)
344 || fabs (dy) < 0.6 * staff_space)
347 vertically center in space.
349 conf->center_tie_vertically (details);
354 conf->dir_ * staff_space * (- 0.3);
359 Real where = 0.5 * conf->dir_;
361 Real rounding_dy = (where - middle[Y_AXIS]);
362 conf->delta_y_ = rounding_dy;
364 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
365 + conf->position_ * staff_space * 0.5
367 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
369 if (Staff_symbol_referencer::staff_radius (me) > abs (conf->head_position_) / 2)
370 conf->position_ += 2 * conf->dir_;
372 conf->position_ += conf->dir_;
379 Real half_space = 0.5 * staff_space;
380 Real y = conf->position_ * half_space;
382 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
383 conf->attachment_x_.widen (-gap);
389 Tie::set_default_control_points (Grob *me_grob)
391 Spanner *me = dynamic_cast<Spanner*> (me_grob);
393 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
394 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
396 Tie_configuration conf;
397 if (!get_grob_direction (me))
398 set_grob_direction (me, get_default_dir (me));
400 int tie_position = (int) Tie::get_position (me);
401 conf.position_ = tie_position;
405 get_configuration (me, common, &conf, 0, details);
406 set_control_points (me, common, conf, details);
410 Tie::set_control_points (Grob *me,
412 Tie_configuration const &conf,
413 Tie_details const &details
416 Bezier b = conf.get_bezier (details);
417 b.scale (1, conf.dir_);
418 b.translate (Offset (conf.attachment_x_[LEFT]
419 - me->relative_coordinate (common, X_AXIS),
420 0.5 * conf.position_ * details.staff_space_
424 SCM controls = SCM_EOL;
425 for (int i = 4; i--;)
427 if (!b.control_[i].is_sane ())
428 programming_error ("Insane offset");
429 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
431 me->set_property ("control-points", controls);
436 MAKE_SCHEME_CALLBACK (Tie, print, 1);
438 Tie::print (SCM smob)
440 Grob *me = unsmob_grob (smob);
442 if (CENTER == get_grob_direction (me))
445 if (!get_grob_direction (me))
446 me->programming_error ("Tie direction not set.");
448 SCM cp = me->get_property ("control-points");
449 if (!scm_is_pair (cp))
452 UGH. dependency tracking!
454 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
456 Tie_column::set_directions (me->get_parent (Y_AXIS));
459 cp = me->get_property ("control-points");
462 if (!scm_is_pair (cp))
464 set_default_control_points (me);
465 cp = me->get_property ("control-points");
468 if (!scm_is_pair (cp))
469 return Stencil ().smobbed_copy ();
471 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
472 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
473 Real thick = base_thick * staff_thick;
477 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
479 b.control_[i] = ly_scm2offset (scm_car (s));
485 SCM p = me->get_property ("dash-period");
486 SCM f = me->get_property ("dash-fraction");
487 if (scm_is_number (p) && scm_is_number (f))
488 a = Lookup::dashed_slur (b,
490 robust_scm2double (p, 1.0),
491 robust_scm2double (f, 0));
494 get_grob_direction (me) * staff_thick,
497 return a.smobbed_copy ();
503 "A tie connecting two noteheads.\n",