source file of the GNU LilyPond music typesetter
- (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
-#include <math.h>
+
+#include "tie.hh"
+
#include "spanner.hh"
#include "lookup.hh"
-#include "paper-def.hh"
-#include "tie.hh"
+#include "output-def.hh"
#include "rhythmic-head.hh"
#include "bezier.hh"
#include "paper-column.hh"
-#include "debug.hh"
+#include "warn.hh"
#include "staff-symbol-referencer.hh"
#include "directional-element-interface.hh"
-#include "molecule.hh"
-#include "bezier-bow.hh"
+#include "bezier.hh"
#include "stem.hh"
#include "note-head.hh"
+#include "tie-column.hh"
-/*
- tie: Connect two noteheads.
-
- What if we have
-
- c4 ~ \clef bass ; c4 or
-
- c4 \staffchange c4
-
- do we have non-horizontal ties then?
- */
-
-
-void
-Tie::set_head (Score_element*me,Direction d, Item * head_l)
+int
+Tie::compare (Grob *const &s1,
+ Grob *const &s2)
{
- assert (!head (me,d));
- index_set_cell (me->get_elt_property ("heads"), d, head_l->self_scm ());
-
- dynamic_cast<Spanner*> (me)->set_bound (d, head_l);
- me->add_dependency (head_l);
+ return sign (Tie::get_position (s1) - Tie::get_position (s2));
}
void
-Tie::set_interface (Score_element*me)
+Tie::set_head (Grob *me, Direction d, Grob *h)
{
- me->set_elt_property ("heads", gh_cons (SCM_EOL, SCM_EOL));
- me->set_interface (ly_symbol2scm ("tie-interface"));
+ dynamic_cast<Spanner *> (me)->set_bound (d, h);
+ me->add_dependency (h);
}
-bool
-Tie::has_interface (Score_element*me)
+Grob *
+Tie::head (Grob *me, Direction d)
{
- return me->has_interface (ly_symbol2scm ("tie-interface"));
+ Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
+ if (Note_head::has_interface (it))
+ return it;
+ else
+ return 0;
}
-Score_element*
-Tie::head (Score_element*me, Direction d)
+int
+Tie::get_column_rank (Grob *me, Direction d)
{
- SCM c = me->get_elt_property ("heads");
- c = index_cell (c, d);
+ Spanner *span = dynamic_cast<Spanner *> (me);
+ Grob *h = head (me, d);
+ if (!h)
+ h = span->get_bound (d);
- return unsmob_element (c);
+ Grob *col = dynamic_cast<Item *> (h)->get_column ();
+ return Paper_column::get_rank (col);
}
-Real
-Tie::position_f (Score_element*me)
+int
+Tie::get_position (Grob *me)
{
- Direction d = head (me,LEFT) ? LEFT:RIGHT;
- return Staff_symbol_referencer::position_f (head (me,d));
+ Direction d = head (me, LEFT) ? LEFT : RIGHT;
+ return (int) Staff_symbol_referencer::get_position (head (me, d));
}
-
/*
+ Default: Put the tie oppositie of the stem [Wanske p231]
+
+ In case of chords: Tie_column takes over
+
The direction of the Tie is more complicated (See [Ross] p136 and
- further), the case of multiple ties is handled by Tie_column.
+ further).
+
+ (what about linebreaks? )
*/
Direction
-Tie::get_default_dir (Score_element*me)
+Tie::get_default_dir (Grob *me)
{
- Item * sl = head(me,LEFT) ? Rhythmic_head::stem_l (head (me,LEFT)) :0;
- Item * sr = head(me,RIGHT) ? Rhythmic_head::stem_l (head (me,RIGHT)) :0;
-
-
+ Item *sl = head (me, LEFT) ? Rhythmic_head::get_stem (head (me, LEFT)) : 0;
+ Item *sr = head (me, RIGHT) ? Rhythmic_head::get_stem (head (me, RIGHT)) : 0;
if (sl && sr)
{
- if (Directional_element_interface (sl).get () == UP
- && Directional_element_interface (sr).get () == UP)
+ if (get_grob_direction (sl) == UP
+ && get_grob_direction (sr) == UP)
return DOWN;
}
else if (sl || sr)
{
Item *s = sl ? sl : sr;
- return - Directional_element_interface (s). get ();
+ return -get_grob_direction (s);
}
-
return UP;
}
-
-SCM
-Tie::get_control_points (SCM smob)
-{
- Spanner*me = dynamic_cast<Spanner*> (unsmob_element (smob));
- Direction headdir = CENTER;
- if (head (me,LEFT))
- headdir = LEFT;
- else if (head(me,RIGHT))
- headdir = RIGHT;
- else
+void
+Tie::set_direction (Grob *me)
+{
+ if (!get_grob_direction (me))
{
- programming_error ("Tie without heads.");
- me->suicide ();
- return SCM_UNSPECIFIED;
+ if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
+ Tie_column::set_directions (me->get_parent (Y_AXIS));
+ else
+ set_grob_direction (me, Tie::get_default_dir (me));
}
-
- if (!Directional_element_interface (me).get ())
- Directional_element_interface (me).set (Tie::get_default_dir (me));
-
- Real staff_space = Staff_symbol_referencer::staff_space (me);
-
- Real x_gap_f = me->paper_l ()->get_var ("tie_x_gap");
-
- Score_element* commonx = me->common_refpoint (me->get_bound (LEFT), X_AXIS);
- commonx = me->common_refpoint (me->get_bound (RIGHT), X_AXIS);
-
- Score_element* l = me->get_bound (LEFT);
- Score_element* r = me->get_bound (RIGHT);
-
- Real left_x;
- if (Note_head::has_interface (me->get_bound (LEFT)))
- left_x = l->extent (X_AXIS)[RIGHT] + x_gap_f;
- else
- left_x = l->extent (X_AXIS).length () / 2;
+}
- Real width;
- if (Note_head::has_interface (me->get_bound (LEFT))
- && Note_head::has_interface (me->get_bound (RIGHT)))
+Interval
+Tie::get_default_attachments (Spanner *me, Grob *common, Real gap,
+ int *staff_position,
+ bool *in_between
+ )
+{
+ Real staff_space = Staff_symbol_referencer::staff_space (me);
+ Direction dir = get_grob_direction (me);
+ Interval attachments;
+ Direction d = LEFT;
+ do
{
- width = r->relative_coordinate (commonx, X_AXIS)
- + r->extent (X_AXIS)[LEFT]
- - l->relative_coordinate (commonx, X_AXIS)
- - l->extent (X_AXIS)[RIGHT]
- -2 * x_gap_f;
+ attachments[d]
+ = robust_relative_extent (me->get_bound (d),
+ common,
+ X_AXIS)[-d]
+ - gap * d;
}
- else
+ while (flip (&d) != LEFT);
+
+ if (attachments.length () < 0.6 * staff_space)
{
- if (Note_head::has_interface (me->get_bound (LEFT)))
- width = r->relative_coordinate (commonx, X_AXIS)
- - l->relative_coordinate (commonx, X_AXIS)
- - l->extent (X_AXIS)[RIGHT]
- - 2 * x_gap_f;
- else
- width = r->relative_coordinate (commonx, X_AXIS)
- - l->extent (X_AXIS).length () / 2
- + r->extent (X_AXIS)[LEFT]
- - l->relative_coordinate (commonx, X_AXIS)
- - 2 * x_gap_f;
+ /*
+ Let short ties start over note heads, instead of between.
+ */
+ Drul_array<bool> allow (true, true);
+
+ Direction d = LEFT;
+ do {
+ if (Note_head::has_interface (me->get_bound (d)))
+ {
+ Grob *stem = unsmob_grob (me->get_bound (d)->get_object ("stem"));
+ if (get_grob_direction (stem) == dir
+ && -d == dir)
+ allow[d] = false;
+ }
+ } while (flip (&d) != LEFT);
+
+ if (allow[LEFT] && allow[RIGHT])
+ {
+ *staff_position += dir;
+ do
+ {
+ if (Note_head::has_interface (me->get_bound (d)))
+ {
+ Interval extent
+ = robust_relative_extent (me->get_bound (d),
+ common, X_AXIS);
+
+ attachments[d] = extent.linear_combination (- 0.5 * d);
+ *in_between = false;
+ }
+ }
+ while (flip (&d) != LEFT);
+ }
}
-
- Direction dir = Directional_element_interface (me).get();
-
- Real h_inf = me->paper_l ()->get_var ("tie_height_limit_factor") * staff_space;
- Real r_0 = me->paper_l ()->get_var ("tie_ratio");
+ return attachments;
+}
- Bezier b = slur_shape (width, h_inf, r_0);
+Interval
+get_skyline_attachment (Drul_array< Array < Skyline_entry > > const &skylines,
+ Real y)
+{
+ Interval attachments;
+ Direction d = LEFT;
+ do
+ {
+ attachments[d] = skyline_height (skylines[d], y, -d);
+ }
+ while (flip (&d) != LEFT);
- Offset leave_dir = b.control_[1] - b.control_[0];
-
- Real dx = (head (me, headdir)->extent (X_AXIS).length () + x_gap_f)/2.0;
- Real max_gap = leave_dir[Y_AXIS] * dx / leave_dir[X_AXIS];
+ return attachments;
+}
+
+void
+Tie::get_configuration (Grob *me_grob, Grob *common,
+ Tie_configuration *conf,
+ Drul_array< Array < Skyline_entry > > const *skylines,
+ Tie_details const &details
+ )
+{
+ Spanner *me = dynamic_cast<Spanner*> (me_grob);
+ if (!head (me, LEFT) && !head (me, RIGHT))
+ {
+ programming_error ("tie without heads");
+ me->suicide ();
+ return ;
+ }
/*
- for small ties (t small) we want to start in the Y-center (so dy = 0), for
- large ties, the tie should appear to come from the center of the
- head, so dy = max_gap
-
- maybe use a different formula?
+ UGH. Don't mirror Tie_configuration.
+ */
+ conf->head_position_ = (int) Tie::get_position (me);
+ if (!conf->dir_)
+ conf->dir_ = get_grob_direction (me);
+ if (!conf->dir_)
+ conf->dir_ = get_default_dir (me);
+
- TODO: what if 2 heads have different size.
+ Real staff_space = details.staff_space_;
+ bool in_between = true;
+ Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
- TODO: for small ties, it is better to start over the heads
- iso. next to the heads.
- */
- Real t = (width / staff_space - 5.0); // ugh.
- Real dy = t > 0 ? max_gap * sqr (t / (1 + t)) : 0.0;
+ if (conf->attachment_x_.is_empty())
+ {
+ if (!skylines)
+ conf->attachment_x_ = get_default_attachments (me, common, gap,
+ &conf->position_,
+ &in_between);
+ else
+ {
+ Real y = staff_space * 0.5 * conf->position_;
+ conf->attachment_x_ = get_skyline_attachment (*skylines, y);
+ conf->attachment_x_.widen (-gap);
+ }
+ }
- Real ypos = Tie::position_f (me) * staff_space/2 + dir * dy;
+ Bezier b = slur_shape (conf->attachment_x_.length(),
+ details.height_limit_,
+ details.ratio_);
+ b.scale (1, conf->dir_);
+
+ Offset middle = b.curve_point (0.5);
+ Offset edge = b.curve_point (0.0);
+ conf->position_ = int (rint (conf->position_));
+
+ Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
+ bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) conf->position_));
+ bool fits_in_space =
+ (dy < 0.6 * staff_space);
+
/*
- todo: prevent ending / staffline collision.
-
- todo: tie / stem collision
+ Avoid dot
*/
+ Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
+ int dot_pos = left_dot
+ ? int (Staff_symbol_referencer::get_position (left_dot))
+ : 0;
+ if (left_dot
+ && (conf->position_ == dot_pos
+ || conf->position_ + conf->dir_ == dot_pos))
+ {
+ conf->position_ += conf->dir_;
+ in_space = !in_space;
- b = slur_shape(width,h_inf, r_0);
- b.scale (1, dir);
- b.translate (Offset (left_x, ypos));
+ if (skylines)
+ {
+ Real y = staff_space * 0.5 * conf->position_;
+ conf->attachment_x_ = get_skyline_attachment (*skylines, y);
+ conf->attachment_x_.widen (-gap);
+ Bezier b = slur_shape (conf->attachment_x_.length(),
+ details.height_limit_,
+ details.ratio_);
+ Offset middle = b.curve_point (0.5);
+ Offset edge = b.curve_point (0.0);
+ dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
+ fits_in_space =
+ (dy < 0.6 * staff_space);
+ }
+ }
-
/*
- Avoid colliding of the horizontal part with stafflines.
-
- should do me for slurs as well.
+ Avoid flag.
+ */
+ Grob *left_stem = unsmob_grob (me->get_bound (LEFT)->get_object ("stem"));
+ if (left_stem)
+ {
+ Stencil flag = Stem::get_translated_flag (left_stem);
+ Real y = conf->position_ * staff_space * 0.5;
+ if (flag.extent (Y_AXIS).contains (y))
+ {
+ conf->position_ += conf->dir_;
+ in_space = !in_space;
+ }
+ }
- */
- Array<Real> horizontal (b.solve_derivative (Offset (1,0)));
- if (horizontal.size ())
+ if (in_space != fits_in_space)
{
+ if (in_space)
+ {
+ conf->position_ += conf->dir_;
+ }
+ else
+ {
+ in_space = true;
+ conf->position_ += conf->dir_;
+ }
+
/*
- ugh. Doesnt work for non-horizontal curves.
+ ugh: code dup.
*/
- Real y = b.curve_point (horizontal[0])[Y_AXIS];
-
- Real ry = rint (y/staff_space) * staff_space;
- Real diff = ry - y;
- Real newy = y;
- if (fabs (y) <= Staff_symbol_referencer::staff_radius (me)
- && fabs (diff) < me->paper_l ()->get_var ("tie_staffline_clearance"))
+ if (skylines)
{
- newy = ry - 0.5 * staff_space * sign (diff) ;
+ Real y = staff_space * 0.5 * conf->position_;
+ conf->attachment_x_ = get_skyline_attachment (*skylines, y);
+ conf->attachment_x_.widen (-gap);
+
+ Bezier b = slur_shape (conf->attachment_x_.length(),
+ details.height_limit_,
+ details.ratio_);
+ Offset middle = b.curve_point (0.5);
+ Offset edge = b.curve_point (0.0);
+ dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
+ fits_in_space =
+ (dy < 0.6 * staff_space);
}
+ }
+
- Real y0 = b.control_ [0][Y_AXIS];
- b.control_[2][Y_AXIS] =
- b.control_[1][Y_AXIS] =
- (b.control_[1][Y_AXIS] - y0) * ((newy - y0) / (y - y0)) + y0;
+ /*
+ Putting larger in-space ties next to the notes forces
+ the edges to be opposite (Y-wise) to the tie direction.
+ */
+ if (conf->position_ == conf->head_position_
+ && in_space
+ && Staff_symbol_referencer::staff_radius (me) > fabs (conf->position_) / 2
+ && dy > 0.3 * staff_space)
+ {
+ conf->position_ += 2 * conf->dir_;
}
- else
- programming_error ("Tie is nowhere horizontal");
+ if (!in_between
+ && in_space
+ && fabs (conf->position_ - conf->head_position_) <= 1)
+ conf->position_ += 2*conf->dir_;
+
+
+ if (in_space)
+ {
+ if ((fabs (conf->position_ - conf->head_position_) <= 1
+ && fabs (dy) < 0.45 * staff_space)
+ || fabs (dy) < 0.6 * staff_space)
+ {
+ /*
+ vertically center in space.
+ */
+ conf->center_tie_vertically (details);
+ }
+ else
+ {
+ conf->delta_y_ =
+ conf->dir_ * staff_space * (- 0.3);
+ }
+ }
+ else
+ {
+ Real where = 0.5 * conf->dir_;
+
+ Real rounding_dy = (where - middle[Y_AXIS]);
+ conf->delta_y_ = rounding_dy;
+
+ if (conf->dir_ * (b.curve_point (0.0)[Y_AXIS]
+ + conf->position_ * staff_space * 0.5
+ + conf->delta_y_) <
+ conf->dir_ * conf->head_position_ * 0.5 * staff_space)
+ {
+ if (Staff_symbol_referencer::staff_radius (me) > fabs (conf->head_position_) / 2)
+ conf->position_ += 2 * conf->dir_;
+ else
+ conf->position_ += conf->dir_;
+ }
+ }
- SCM controls = SCM_EOL;
- for (int i= 4; i--;)
- controls = gh_cons ( ly_offset2scm (b.control_[i]), controls);
- return controls;
+ if (skylines)
+ {
+ Real half_space = 0.5 * staff_space;
+ Real y = conf->position_ * half_space;
+
+ conf->attachment_x_ = get_skyline_attachment (*skylines, y);
+ conf->attachment_x_.widen (-gap);
+ }
}
-MAKE_SCHEME_CALLBACK(Tie,set_spacing_rods);
-/*
- TODO: set minimum distances for begin/end of line
- */
-SCM
-Tie::set_spacing_rods (SCM smob)
+void
+Tie::set_default_control_points (Grob *me_grob)
{
- Score_element*me = unsmob_element (smob);
- Spanner*sp = dynamic_cast<Spanner*> (me);
- Rod r;
+ Spanner *me = dynamic_cast<Spanner*> (me_grob);
+ Grob *common = me;
+ common = me->get_bound (LEFT)->common_refpoint (common, X_AXIS);
+ common = me->get_bound (RIGHT)->common_refpoint (common, X_AXIS);
+
+ Tie_configuration conf;
+ if (!get_grob_direction (me))
+ set_grob_direction (me, get_default_dir (me));
- r.item_l_drul_ [LEFT]=sp->get_bound (LEFT);
- r.item_l_drul_ [RIGHT]=sp->get_bound (RIGHT);
+ int tie_position = (int) Tie::get_position (me);
+ conf.position_ = tie_position;
- r.distance_f_
- = gh_scm2double (me->get_elt_property ("minimum-length"))
- * me->paper_l ()->get_var ("staffspace");
- r.add_to_cols ();
- return SCM_UNSPECIFIED;
+ Tie_details details;
+ details.init (me);
+ get_configuration (me, common, &conf, 0, details);
+ set_control_points (me, common, conf, details);
}
-MAKE_SCHEME_CALLBACK(Tie,brew_molecule);
+void
+Tie::set_control_points (Grob *me,
+ Grob *common,
+ Tie_configuration const &conf,
+ Tie_details const &details
+ )
+{
+ Bezier b = conf.get_bezier (details);
+ b.scale (1, conf.dir_);
+ b.translate (Offset (conf.attachment_x_[LEFT]
+ - me->relative_coordinate (common, X_AXIS),
+ 0.5 * conf.position_ * details.staff_space_
+ + conf.delta_y_
+ ));
+
+ SCM controls = SCM_EOL;
+ for (int i = 4; i--;)
+ {
+ if (!b.control_[i].is_sane ())
+ programming_error ("Insane offset");
+ controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
+ }
+ me->set_property ("control-points", controls);
+}
+
+
+
+MAKE_SCHEME_CALLBACK (Tie, print, 1);
SCM
-Tie::brew_molecule (SCM smob)
+Tie::print (SCM smob)
{
- Score_element*me = unsmob_element (smob);
+ Grob *me = unsmob_grob (smob);
+
+ if (CENTER == get_grob_direction (me))
+ set_direction (me);
- SCM cp = me->get_elt_property ("control-points");
- if (cp == SCM_EOL)
+ if (!get_grob_direction (me))
+ me->programming_error ("Tie direction not set.");
+
+ SCM cp = me->get_property ("control-points");
+ if (!scm_is_pair (cp))
{
- cp = get_control_points (smob);
- me->set_elt_property ("control-points", cp);
+ /*
+ UGH. dependency tracking!
+ */
+ if (Tie_column::has_interface (me->get_parent (Y_AXIS)))
+ {
+ Tie_column::set_directions (me->get_parent (Y_AXIS));
+ }
+
+ cp = me->get_property ("control-points");
}
- Real thick =
- gh_scm2double (me->get_elt_property ("thickness"))
- * me->paper_l ()->get_var ("stafflinethickness");
+ if (!scm_is_pair (cp))
+ {
+ set_default_control_points (me);
+ cp = me->get_property ("control-points");
+ }
+
+ if (!scm_is_pair (cp))
+ return Stencil ().smobbed_copy ();
+
+ Real staff_thick = Staff_symbol_referencer::line_thickness (me);
+ Real base_thick = robust_scm2double (me->get_property ("thickness"), 1);
+ Real thick = base_thick * staff_thick;
Bezier b;
int i = 0;
- for (SCM s= cp; s != SCM_EOL; s = gh_cdr (s))
+ for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
{
- b.control_[i] = ly_scm2offset (gh_car (s));
+ b.control_[i] = ly_scm2offset (scm_car (s));
i++;
}
-
- Molecule a = me->lookup_l ()->slur (b, Directional_element_interface (me).get () * thick, thick);
-
- return a.create_scheme ();
-}
+ Stencil a;
+
+ SCM p = me->get_property ("dash-period");
+ SCM f = me->get_property ("dash-fraction");
+ if (scm_is_number (p) && scm_is_number (f))
+ a = Lookup::dashed_slur (b,
+ thick,
+ robust_scm2double (p, 1.0),
+ robust_scm2double (f, 0));
+ else
+ a = Lookup::slur (b,
+ get_grob_direction (me) * staff_thick,
+ thick);
+
+ return a.smobbed_copy ();
+}
+ADD_INTERFACE (Tie,
+ "tie-interface",
+
+ "A tie connecting two noteheads.\n",
+
+ /* properties */
+ "control-points "
+ "dash-fraction "
+ "dash-period "
+ "details "
+ "direction "
+ "thickness "
+ "x-gap ");