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)
201 Spanner *me = dynamic_cast<Spanner*> (me_grob);
202 if (!head (me, LEFT) && !head (me, RIGHT))
204 programming_error ("tie without heads");
210 UGH. Don't mirror Tie_configuration.
212 conf->head_position_ = (int) Tie::get_position (me);
214 conf->dir_ = get_grob_direction (me);
216 conf->dir_ = get_default_dir (me);
218 Real staff_space = problem.details_.staff_space_;
219 bool in_between = true;
220 Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
222 if (conf->attachment_x_.is_empty())
226 conf->attachment_x_ = get_default_attachments (me, common, gap,
228 &in_between, problem.details_);
230 Real y = staff_space * 0.5 * conf->position_;
231 conf->attachment_x_ = problem.get_attachment (y);
232 conf->attachment_x_.widen (-gap);
235 Bezier b = slur_shape (conf->attachment_x_.length(),
236 problem.details_.height_limit_,
237 problem.details_.ratio_);
238 b.scale (1, conf->dir_);
240 Offset middle = b.curve_point (0.5);
241 Offset edge = b.curve_point (0.0);
243 conf->position_ = int (rint (conf->position_));
245 Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
246 bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
248 (dy < 0.6 * staff_space);
253 Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
254 int dot_pos = left_dot
255 ? int (Staff_symbol_referencer::get_position (left_dot))
258 && (conf->position_ == dot_pos
259 || conf->position_ + conf->dir_ == dot_pos))
261 conf->position_ += conf->dir_;
262 in_space = !in_space;
264 if (conf->position_ == dot_pos)
266 conf->position_ += conf->dir_;
267 in_space = !in_space;
270 Real y = staff_space * 0.5 * conf->position_;
271 conf->attachment_x_ = problem.get_attachment (y);
272 conf->attachment_x_.widen (-gap);
273 Bezier b = slur_shape (conf->attachment_x_.length(),
274 problem.details_.height_limit_,
275 problem.details_.ratio_);
276 Offset middle = b.curve_point (0.5);
277 Offset edge = b.curve_point (0.0);
278 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
280 (dy < 0.6 * staff_space);
286 Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
289 Stencil flag = Stem::get_translated_flag (left_stem);
290 Real y = conf->position_ * staff_space * 0.5;
291 if (flag.extent (Y_AXIS).contains (y))
293 conf->position_ += conf->dir_;
294 in_space = !in_space;
298 if (in_space != fits_in_space)
302 conf->position_ += conf->dir_;
307 conf->position_ += conf->dir_;
313 Real y = staff_space * 0.5 * conf->position_;
314 conf->attachment_x_ = problem.get_attachment (y);
315 conf->attachment_x_.widen (-gap);
317 Bezier b = slur_shape (conf->attachment_x_.length(),
318 problem.details_.height_limit_,
319 problem.details_.ratio_);
320 Offset middle = b.curve_point (0.5);
321 Offset edge = b.curve_point (0.0);
322 dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
324 (dy < 0.6 * staff_space);
329 Putting larger in-space ties next to the notes forces
330 the edges to be opposite (Y-wise) to the tie direction.
332 if (conf->position_ == conf->head_position_
334 && Staff_symbol_referencer::staff_radius (me) > abs (conf->position_) / 2
335 && dy > 0.3 * staff_space
338 conf->position_ += 2 * conf->dir_;
343 && abs (conf->position_ - conf->head_position_) <= 1)
345 int amount = conf->dir_;
346 if (sign (conf->position_) != conf->dir_
347 || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
350 conf->position_ += amount;
355 if ((sign (conf->position_) != conf->dir_
356 || conf->position_ < Staff_symbol_referencer::staff_radius (me) * 2)
358 ((abs (conf->position_ - conf->head_position_) <= 1
359 && fabs (dy) < 0.45 * staff_space)
360 || fabs (dy) < 0.6 * staff_space))
363 vertically center in space.
365 conf->center_tie_vertically (problem.details_);
370 conf->dir_ * staff_space * (- 0.3);
375 Real where = 0.5 * conf->dir_;
377 Real rounding_dy = (where - middle[Y_AXIS]);
378 conf->delta_y_ = rounding_dy;
380 if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
381 + conf->position_ * staff_space * 0.5
383 conf->dir_ * conf->head_position_ * 0.5 * staff_space)
385 if (Staff_symbol_referencer::staff_radius (me) > abs (conf->head_position_) / 2)
386 conf->position_ += 2 * conf->dir_;
388 conf->position_ += conf->dir_;
393 Real half_space = 0.5 * staff_space;
394 Real y = conf->position_ * half_space;
396 conf->attachment_x_ = problem.get_attachment (y);
397 conf->attachment_x_.widen (-gap);
402 Tie::set_default_control_points (Grob *me_grob)
404 Spanner *me = dynamic_cast<Spanner*> (me_grob);
406 common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
407 common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
409 Tie_formatting_problem problem;
410 problem.from_tie (me);
412 // get_configuration (me, &conf, problem);
413 int tie_position = (int) Tie::get_position (me);
414 Tie_configuration conf
415 = problem.find_optimal_tie_configuration (tie_position, get_grob_direction (me));
416 set_control_points (me, problem.common_x_refpoint (),
417 conf, problem.details_);
421 Tie::set_control_points (Grob *me,
423 Tie_configuration const &conf,
424 Tie_details const &details
427 Bezier b = conf.get_bezier (details);
428 b.scale (1, conf.dir_);
429 b.translate (Offset (conf.attachment_x_[LEFT]
430 - me->relative_coordinate (common, X_AXIS),
431 0.5 * conf.position_ * details.staff_space_
435 SCM controls = SCM_EOL;
436 for (int i = 4; i--;)
438 if (!b.control_[i].is_sane ())
439 programming_error ("Insane offset");
440 controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
442 me->set_property ("control-points", controls);
445 MAKE_SCHEME_CALLBACK(Tie, calc_control_points, 1);
447 Tie::calc_control_points (SCM smob)
449 Grob *me = unsmob_grob (smob);
451 // trigger Tie-column
452 (void) get_grob_direction (me);
454 Grob *yparent = me->get_parent (Y_AXIS);
455 if (Tie_column::has_interface (yparent)
456 && unsmob_grob_array (yparent->get_object ("ties"))
457 && unsmob_grob_array (yparent->get_object ("ties"))->size () > 1)
459 /* trigger positioning. */
460 (void) yparent->get_property ("positioning-done");
463 if (!scm_is_pair (me->get_property ("control-points")))
465 set_default_control_points (me);
468 return SCM_UNSPECIFIED;
472 MAKE_SCHEME_CALLBACK (Tie, print, 1);
474 Tie::print (SCM smob)
476 Grob *me = unsmob_grob (smob);
478 SCM cp = me->get_property ("control-points");
480 Real staff_thick = Staff_symbol_referencer::line_thickness (me);
481 Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
482 Real thick = base_thick * staff_thick;
486 for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
488 b.control_[i] = ly_scm2offset (scm_car (s));
494 SCM p = me->get_property ("dash-period");
495 SCM f = me->get_property ("dash-fraction");
496 if (scm_is_number (p) && scm_is_number (f))
497 a = Lookup::dashed_slur (b,
499 robust_scm2double (p, 1.0),
500 robust_scm2double (f, 0));
503 get_grob_direction (me) * staff_thick,
506 return a.smobbed_copy ();
512 "A tie connecting two noteheads. \n\n"