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>
16 #include "output-def.hh"
17 #include "rhythmic-head.hh"
19 #include "paper-column.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "directional-element-interface.hh"
25 #include "note-head.hh"
26 #include "tie-column.hh"
29 Tie::compare (Grob *const &s1,
32 return sign (Tie::get_position (s1) - Tie::get_position (s2));
36 Tie::set_head (Grob *me, Direction d, Grob *h)
38 dynamic_cast<Spanner *> (me)->set_bound (d, h);
39 me->add_dependency (h);
43 Tie::head (Grob *me, Direction d)
45 Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
46 if (Note_head::has_interface (it))
53 Tie::get_column_rank (Grob *me, Direction d)
55 Spanner *span = dynamic_cast<Spanner *> (me);
56 Grob *h = head (me, d);
58 h = span->get_bound (d);
60 Grob *col = dynamic_cast<Item *> (h)->get_column ();
61 return Paper_column::get_rank (col);
65 Tie::get_position (Grob *me)
67 Direction d = head (me, LEFT) ? LEFT : RIGHT;
68 return (int) Staff_symbol_referencer::get_position (head (me, d));
72 Default: Put the tie oppositie of the stem [Wanske p231]
74 In case of chords: Tie_column takes over
76 The direction of the Tie is more complicated (See [Ross] p136 and
79 (what about linebreaks? )
82 Tie::get_default_dir (Grob *me)
84 Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
85 Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
88 if (get_grob_direction (sl) == UP
89 && get_grob_direction (sr) == UP)
94 Item *s = sl ? sl : sr;
95 return -get_grob_direction (s);
102 Tie::set_direction (Grob *me)
104 if (!get_grob_direction (me))
106 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
107 Tie_column::set_directions (me->get_parent (Y_AXIS));
109 set_grob_direction (me, Tie::get_default_dir (me));
114 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
119 Real staff_space = Staff_symbol_referencer::staff_space (me);
120 Direction dir = get_grob_direction (me);
121 Interval attachments;
126 = robust_relative_extent (me->get_bound (d),
131 while (flip (&d) != LEFT);
133 if (attachments.length () < 0.6 * staff_space)
136 Let short ties start over note heads, instead of between.
138 Drul_array<bool> allow (true, true);
142 if (Note_head::has_interface (me->get_bound (d)))
144 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
145 if (get_grob_direction (stem) == dir
149 } while (flip (&d) != LEFT);
151 if (allow[LEFT] && allow[RIGHT])
153 *staff_position += dir;
156 if (Note_head::has_interface (me->get_bound (d)))
159 = robust_relative_extent (me->get_bound (d),
162 attachments[d] = extent.linear_combination (- 0.5 * d);
166 while (flip (&d) != LEFT);
174 get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
177 Interval attachments;
181 attachments[d] = skyline_height (skylines[d], y, -d);
183 while (flip (&d) != LEFT);
189 Tie::get_configuration (Grob *me_grob, Grob *common,
190 Tie_configuration *conf,
191 Drul_array< Array < Skyline_entry > > const *skylines,
192 Tie_details const &details
195 Spanner *me = dynamic_cast<Spanner*> (me_grob);
196 if (!head (me, LEFT) && !head (me, RIGHT))
198 programming_error ("tie without heads");
204 UGH. Don't mirror Tie_configuration.
206 conf->head_position_ = (int) Tie::get_position (me);
208 conf->dir_ = get_grob_direction (me);
210 conf->dir_ = get_default_dir (me);
213 Real staff_space = details.staff_space_;
214 bool in_between = true;
215 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
217 if (conf->attachment_x_.is_empty())
220 conf->attachment_x_ = get_default_attachments (me, common, gap,
225 Real y = staff_space * 0.5 * conf->position_;
226 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
227 conf->attachment_x_.widen (-gap);
231 Bezier b = slur_shape (conf->attachment_x_.length(),
232 details.height_limit_,
234 b.scale (1, conf->dir_);
236 Offset middle = b.curve_point (0.5);
237 Offset edge = b.curve_point (0.0);
239 conf->position_ = int (rint (conf->position_));
241 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
242 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
244 (dy < 0.6 * staff_space);
249 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
250 int dot_pos = left_dot
251 ? int (Staff_symbol_referencer::get_position (left_dot))
254 && (conf->position_ == dot_pos
255 || conf->position_ + conf->dir_ == dot_pos))
257 conf->position_ += conf->dir_;
258 in_space = !in_space;
262 Real y = staff_space * 0.5 * conf->position_;
263 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
264 conf->attachment_x_.widen (-gap);
265 Bezier b = slur_shape (conf->attachment_x_.length(),
266 details.height_limit_,
268 Offset middle = b.curve_point (0.5);
269 Offset edge = b.curve_point (0.0);
270 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
272 (dy < 0.6 * staff_space);
279 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
282 Stencil flag = Stem::get_translated_flag (left_stem);
283 Real y = conf->position_ * staff_space * 0.5;
284 if (flag.extent (Y_AXIS).contains (y))
286 conf->position_ += conf->dir_;
287 in_space = !in_space;
291 if (in_space != fits_in_space)
295 conf->position_ += conf->dir_;
300 conf->position_ += conf->dir_;
308 Real y = staff_space * 0.5 * conf->position_;
309 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
310 conf->attachment_x_.widen (-gap);
312 Bezier b = slur_shape (conf->attachment_x_.length(),
313 details.height_limit_,
315 Offset middle = b.curve_point (0.5);
316 Offset edge = b.curve_point (0.0);
317 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
319 (dy < 0.6 * staff_space);
325 Putting larger in-space ties next to the notes forces
326 the edges to be opposite (Y-wise) to the tie direction.
328 if (conf->position_ == conf->head_position_
330 && Staff_symbol_referencer::staff_radius (me) > fabs (conf->position_) / 2
331 && dy > 0.3 * staff_space)
333 conf->position_ += 2 * conf->dir_;
338 && fabs (conf->position_ - conf->head_position_) <= 1)
339 conf->position_ += 2*conf->dir_;
344 if ((fabs (conf->position_ - conf->head_position_) <= 1
345 && fabs (dy) < 0.45 * staff_space)
346 || fabs (dy) < 0.6 * staff_space)
349 vertically center in space.
351 conf->center_tie_vertically (details);
356 conf->dir_ * staff_space * (- 0.3);
361 Real where = 0.5 * conf->dir_;
363 Real rounding_dy = (where - middle[Y_AXIS]);
364 conf->delta_y_ = rounding_dy;
366 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
367 + conf->position_ * staff_space * 0.5
369 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
371 if (Staff_symbol_referencer::staff_radius (me) > fabs (conf->head_position_) / 2)
372 conf->position_ += 2 * conf->dir_;
374 conf->position_ += conf->dir_;
381 Real half_space = 0.5 * staff_space;
382 Real y = conf->position_ * half_space;
384 conf->attachment_x_ = get_skyline_attachment (*skylines, y);
385 conf->attachment_x_.widen (-gap);
391 Tie::set_default_control_points (Grob *me_grob)
393 Spanner *me = dynamic_cast<Spanner*> (me_grob);
395 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
396 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
398 Tie_configuration conf;
399 if (!get_grob_direction (me))
400 set_grob_direction (me, get_default_dir (me));
402 int tie_position = (int) Tie::get_position (me);
403 conf.position_ = tie_position;
407 get_configuration (me, common, &conf, 0, details);
408 set_control_points (me, common, conf, details);
412 Tie::set_control_points (Grob *me,
414 Tie_configuration const &conf,
415 Tie_details const &details
418 Bezier b = conf.get_bezier (details);
419 b.scale (1, conf.dir_);
420 b.translate (Offset (conf.attachment_x_[LEFT]
421 - me->relative_coordinate (common, X_AXIS),
422 0.5 * conf.position_ * details.staff_space_
426 SCM controls = SCM_EOL;
427 for (int i = 4; i--;)
429 if (!b.control_[i].is_sane ())
430 programming_error ("Insane offset");
431 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
433 me->set_property ("control-points", controls);
438 MAKE_SCHEME_CALLBACK (Tie, print, 1);
440 Tie::print (SCM smob)
442 Grob *me = unsmob_grob (smob);
444 if (CENTER == get_grob_direction (me))
447 if (!get_grob_direction (me))
448 me->programming_error ("Tie direction not set.");
450 SCM cp = me->get_property ("control-points");
451 if (!scm_is_pair (cp))
454 UGH. dependency tracking!
456 if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
458 Tie_column::set_directions (me->get_parent (Y_AXIS));
461 cp = me->get_property ("control-points");
464 if (!scm_is_pair (cp))
466 set_default_control_points (me);
467 cp = me->get_property ("control-points");
470 if (!scm_is_pair (cp))
471 return Stencil ().smobbed_copy ();
473 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
474 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
475 Real thick = base_thick * staff_thick;
479 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
481 b.control_[i] = ly_scm2offset (scm_car (s));
487 SCM p = me->get_property ("dash-period");
488 SCM f = me->get_property ("dash-fraction");
489 if (scm_is_number (p) && scm_is_number (f))
490 a = Lookup::dashed_slur (b,
492 robust_scm2double (p, 1.0),
493 robust_scm2double (f, 0));
496 get_grob_direction (me) * staff_thick,
499 return a.smobbed_copy ();
505 "A tie connecting two noteheads.\n",