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"
24 #include "tie-formatting-problem.hh"
28 Tie::compare (Grob *const &s1,
31 return sign (Tie::get_position (s1) - Tie::get_position (s2));
35 Tie::set_head (Grob *me, Direction d, Grob *h)
37 dynamic_cast<Spanner *> (me)->set_bound (d, 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)
68 Grob *h = head (me, d);
70 return (int) Staff_symbol_referencer::get_position (h);
72 while (flip (&d) != LEFT);
76 TODO: this is theoretically possible for ties across more than 2
77 systems.. We should look at the first broken copy.
80 programming_error ("Tie without heads. Suicide");
86 Default: Put the tie oppositie of the stem [Wanske p231]
88 In case of chords: Tie_column takes over
90 The direction of the Tie is more complicated (See [Ross] p136 and
93 (what about linebreaks? )
96 Tie::get_default_dir (Grob *me)
98 Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
99 Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
102 if (get_grob_direction (sl) == UP
103 && get_grob_direction (sr) == UP)
108 Item *s = sl ? sl : sr;
109 return -get_grob_direction (s);
116 MAKE_SCHEME_CALLBACK(Tie, calc_direction, 1);
118 Tie::calc_direction (SCM smob)
120 Grob *me = unsmob_grob (smob);
121 Grob *yparent = me->get_parent (Y_AXIS);
122 if (Tie_column::has_interface (yparent)
123 && unsmob_grob_array (yparent->get_object ("ties"))
124 && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
126 /* trigger positioning. */
127 (void) yparent->get_property ("positioning-done");
130 set_grob_direction (me, Tie::get_default_dir (me));
132 return SCM_UNSPECIFIED;
136 Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
139 Tie_details const &details
142 Real staff_space = Staff_symbol_referencer::staff_space (me);
143 Direction dir = get_grob_direction (me);
144 Interval attachments;
149 = robust_relative_extent (me->get_bound (d),
154 while (flip (&d) != LEFT);
156 if (attachments.length () < details.between_length_limit_ * staff_space)
159 Let short ties start over note heads, instead of between.
161 Drul_array<bool> allow (true, true);
165 if (Note_head::has_interface (me->get_bound (d)))
167 Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
168 if (get_grob_direction (stem) == dir
172 } while (flip (&d) != LEFT);
174 if (allow[LEFT] && allow[RIGHT])
176 *staff_position += dir;
179 if (Note_head::has_interface (me->get_bound (d)))
182 = robust_relative_extent (me->get_bound (d),
185 attachments[d] = extent.linear_combination (- 0.5 * d);
189 while (flip (&d) != LEFT);
197 Tie::get_configuration (Grob *me_grob,
198 Tie_configuration *conf,
199 Tie_formatting_problem const &problem,
200 Tie_details const &details
203 Spanner *me = dynamic_cast<Spanner*> (me_grob);
204 if (!head (me, LEFT) && !head (me, RIGHT))
206 programming_error ("tie without heads");
212 UGH. Don't mirror Tie_configuration.
214 conf->head_position_ = (int) Tie::get_position (me);
216 conf->dir_ = get_grob_direction (me);
218 conf->dir_ = get_default_dir (me);
220 Real staff_space = details.staff_space_;
221 bool in_between = true;
222 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
224 if (conf->attachment_x_.is_empty())
228 conf->attachment_x_ = get_default_attachments (me, common, gap,
230 &in_between, details);
232 Real y = staff_space * 0.5 * conf->position_;
233 conf->attachment_x_ = problem.get_attachment (y);
234 conf->attachment_x_.widen (-gap);
237 Bezier b = slur_shape (conf->attachment_x_.length(),
238 details.height_limit_,
240 b.scale (1, conf->dir_);
242 Offset middle = b.curve_point (0.5);
243 Offset edge = b.curve_point (0.0);
245 conf->position_ = int (rint (conf->position_));
247 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
248 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
250 (dy < 0.6 * staff_space);
255 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
256 int dot_pos = left_dot
257 ? int (Staff_symbol_referencer::get_position (left_dot))
260 && (conf->position_ == dot_pos
261 || conf->position_ + conf->dir_ == dot_pos))
263 conf->position_ += conf->dir_;
264 in_space = !in_space;
266 if (conf->position_ == dot_pos)
268 conf->position_ += conf->dir_;
269 in_space = !in_space;
272 Real y = staff_space * 0.5 * conf->position_;
273 conf->attachment_x_ = problem.get_attachment (y);
274 conf->attachment_x_.widen (-gap);
275 Bezier b = slur_shape (conf->attachment_x_.length(),
276 details.height_limit_,
278 Offset middle = b.curve_point (0.5);
279 Offset edge = b.curve_point (0.0);
280 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
282 (dy < 0.6 * staff_space);
288 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
291 Stencil flag = Stem::get_translated_flag (left_stem);
292 Real y = conf->position_ * staff_space * 0.5;
293 if (flag.extent (Y_AXIS).contains (y))
295 conf->position_ += conf->dir_;
296 in_space = !in_space;
300 if (in_space != fits_in_space)
304 conf->position_ += conf->dir_;
309 conf->position_ += conf->dir_;
315 Real y = staff_space * 0.5 * conf->position_;
316 conf->attachment_x_ = problem.get_attachment (y);
317 conf->attachment_x_.widen (-gap);
319 Bezier b = slur_shape (conf->attachment_x_.length(),
320 details.height_limit_,
322 Offset middle = b.curve_point (0.5);
323 Offset edge = b.curve_point (0.0);
324 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
326 (dy < 0.6 * staff_space);
331 Putting larger in-space ties next to the notes forces
332 the edges to be opposite (Y-wise) to the tie direction.
334 if (conf->position_ == conf->head_position_
336 && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
337 && dy > 0.3 * staff_space
340 conf->position_ += 2 * conf->dir_;
345 && abs (conf->position_ - conf->head_position_) <= 1)
347 int amount = conf->dir_;
348 if (sign (conf->position_) != conf->dir_
349 || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
352 conf->position_ += amount;
357 if ((sign (conf->position_) != conf->dir_
358 || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
360 ((abs (conf->position_ - conf->head_position_) <= 1
361 && fabs (dy) < 0.45 * staff_space)
362 || fabs (dy) < 0.6 * staff_space))
365 vertically center in space.
367 conf->center_tie_vertically (details);
372 conf->dir_ * staff_space * (- 0.3);
377 Real where = 0.5 * conf->dir_;
379 Real rounding_dy = (where - middle[Y_AXIS]);
380 conf->delta_y_ = rounding_dy;
382 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
383 + conf->position_ * staff_space * 0.5
385 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
387 if (Staff_symbol_referencer::staff_radius (me) > abs (conf->head_position_) / 2)
388 conf->position_ += 2 * conf->dir_;
390 conf->position_ += conf->dir_;
395 Real half_space = 0.5 * staff_space;
396 Real y = conf->position_ * half_space;
398 conf->attachment_x_ = problem.get_attachment (y);
399 conf->attachment_x_.widen (-gap);
404 Tie::set_default_control_points (Grob *me_grob)
406 Spanner *me = dynamic_cast<Spanner*> (me_grob);
408 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
409 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
411 Tie_configuration conf;
412 int tie_position = (int) Tie::get_position (me);
413 conf.position_ = tie_position;
418 Tie_formatting_problem problem;
419 problem.from_tie (me);
421 get_configuration (me, &conf, problem, details);
422 set_control_points (me, problem.common_x_refpoint (),
427 Tie::set_control_points (Grob *me,
429 Tie_configuration const &conf,
430 Tie_details const &details
433 Bezier b = conf.get_bezier (details);
434 b.scale (1, conf.dir_);
435 b.translate (Offset (conf.attachment_x_[LEFT]
436 - me->relative_coordinate (common, X_AXIS),
437 0.5 * conf.position_ * details.staff_space_
441 SCM controls = SCM_EOL;
442 for (int i = 4; i--;)
444 if (!b.control_[i].is_sane ())
445 programming_error ("Insane offset");
446 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
448 me->set_property ("control-points", controls);
451 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
453 Tie::calc_control_points (SCM smob)
455 Grob *me = unsmob_grob (smob);
457 // trigger Tie-column
458 (void) get_grob_direction (me);
460 Grob *yparent = me->get_parent (Y_AXIS);
461 if (Tie_column::has_interface (yparent)
462 && unsmob_grob_array (yparent->get_object ("ties"))
463 && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
465 /* trigger positioning. */
466 (void) yparent->get_property ("positioning-done");
469 if (!scm_is_pair (me->get_property ("control-points")))
471 set_default_control_points (me);
474 return SCM_UNSPECIFIED;
478 MAKE_SCHEME_CALLBACK (Tie, print, 1);
480 Tie::print (SCM smob)
482 Grob *me = unsmob_grob (smob);
484 SCM cp = me->get_property ("control-points");
486 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
487 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
488 Real thick = base_thick * staff_thick;
492 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
494 b.control_[i] = ly_scm2offset (scm_car (s));
500 SCM p = me->get_property ("dash-period");
501 SCM f = me->get_property ("dash-fraction");
502 if (scm_is_number (p) && scm_is_number (f))
503 a = Lookup::dashed_slur (b,
505 robust_scm2double (p, 1.0),
506 robust_scm2double (f, 0));
509 get_grob_direction (me) * staff_thick,
512 return a.smobbed_copy ();
518 "A tie connecting two noteheads. \n\n"