X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Ftie.cc;h=d40b0c33694dfcbb9a23f4dc6429820759a113aa;hb=105fd7580334c8f42838ac62504c5f290f3428c5;hp=7ce834946c887d185a5b5318e814135ebdd61eb4;hpb=036af34aa44a151b9e67c18e0acccaafdfae9de8;p=lilypond.git diff --git a/lily/tie.cc b/lily/tie.cc index 7ce834946c..d40b0c3369 100644 --- a/lily/tie.cc +++ b/lily/tie.cc @@ -3,93 +3,299 @@ source file of the GNU LilyPond music typesetter - (c) 1997 Han-Wen Nienhuys + (c) 1997--2000 Han-Wen Nienhuys */ +#include +#include "spanner.hh" +#include "lookup.hh" #include "paper-def.hh" #include "tie.hh" +#include "rhythmic-head.hh" +#include "bezier.hh" +#include "paper-column.hh" +#include "debug.hh" +#include "staff-symbol-referencer.hh" +#include "directional-element-interface.hh" +#include "molecule.hh" +#include "bezier-bow.hh" +#include "stem.hh" #include "note-head.hh" -#include "p-col.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(int x_pos, Note_head * head_l) +Tie::set_head (Score_element*me,Direction d, Item * head_l) { - if (x_pos >0) { - assert(!right_head_l_); - right_head_l_ = head_l; - } else { - assert(!left_head_l_); - left_head_l_ = head_l; - } - add_dependency(head_l); + assert (!head (me,d)); + index_set_cell (me->get_elt_property ("heads"), d, head_l->self_scm ()); + + dynamic_cast (me)->set_bound (d, head_l); + me->add_dependency (head_l); } -Tie::Tie() +void +Tie::set_interface (Score_element*me) { - right_head_l_ =0; - left_head_l_ =0; - same_pitch_b_ =false; + me->set_elt_property ("heads", gh_cons (SCM_EOL, SCM_EOL)); + me->set_interface (ly_symbol2scm ("tie-interface")); } -void -Tie::set_default_dir() +bool +Tie::has_interface (Score_element*me) { - int m= (left_head_l_->position_i_ + right_head_l_->position_i_) /2 ; - dir_i_ = (m < 5)? -1:1; // ugh + return me->has_interface (ly_symbol2scm ("tie-interface")); } - -void -Tie::do_add_processing() +Score_element* +Tie::head (Score_element*me, Direction d) { - assert(left_head_l_ && right_head_l_); - left_col_l_ = left_head_l_ -> pcol_l_; - right_col_l_ = right_head_l_ -> pcol_l_; + SCM c = me->get_elt_property ("heads"); + c = index_cell (c, d); + + return unsmob_element (c); } -/** - This is already getting hairy. Should use Note_head *heads[2] - */ -void -Tie::do_post_processing() +Real +Tie::position_f (Score_element*me) +{ + Direction d = head (me,LEFT) ? LEFT:RIGHT; + return Staff_symbol_referencer::position_f (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). +*/ +Direction +Tie::get_default_dir (Score_element*me) { - assert(left_head_l_ || right_head_l_); - left_pos_i_ = (left_head_l_)? - left_head_l_->position_i_ : right_head_l_->position_i_; - right_pos_i_ = (right_head_l_) ? - right_head_l_->position_i_ : left_head_l_->position_i_; - - if ( right_head_l_ && right_head_l_->extremal_i_) { - right_pos_i_ += 2*dir_i_; - right_dx_f_ -= 0.25; - } else - right_dx_f_ -= 0.5; - - if (left_head_l_ && left_head_l_->extremal_i_) { - left_pos_i_ += 2*dir_i_; - left_dx_f_ += 0.25; - } else - left_dx_f_ += 0.5; + 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; + + if (sl && sr) + { + if (Directional_element_interface::get (sl) == UP + && Directional_element_interface::get (sr) == UP) + return DOWN; + } + else if (sl || sr) + { + Item *s = sl ? sl : sr; + return - Directional_element_interface::get (s); + } + + + return UP; +} + + +SCM +Tie::get_control_points (SCM smob) +{ + Spanner*me = dynamic_cast (unsmob_element (smob)); + Direction headdir = CENTER; + if (head (me,LEFT)) + headdir = LEFT; + else if (head(me,RIGHT)) + headdir = RIGHT; + else + { + programming_error ("Tie without heads."); + me->suicide (); + return SCM_UNSPECIFIED; + } + + if (!Directional_element_interface::get (me)) + Directional_element_interface::set (me, 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))) + { + 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; + } + else + { + 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; + } + + Direction dir = Directional_element_interface::get(me); + + Real h_inf = me->paper_l ()->get_var ("tie_height_limit_factor") * staff_space; + Real r_0 = me->paper_l ()->get_var ("tie_ratio"); + + + Bezier b = slur_shape (width, h_inf, r_0); + + 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]; + + /* + 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? + + TODO: what if 2 heads have different size. + + 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; + + Real ypos = Tie::position_f (me) * staff_space/2 + dir * dy; + + /* + todo: prevent ending / staffline collision. + + todo: tie / stem collision + */ + + b = slur_shape(width,h_inf, r_0); + b.scale (1, dir); + b.translate (Offset (left_x, ypos)); + + + /* + Avoid colliding of the horizontal part with stafflines. - if (!right_head_l_) - right_pos_i_ = left_pos_i_; - if (! left_head_l_) - left_pos_i_ = right_pos_i_; + should do me for slurs as well. + + */ + Array horizontal (b.solve_derivative (Offset (1,0))); + if (horizontal.size ()) + { + /* + ugh. Doesnt work for non-horizontal curves. + */ + 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")) + { + newy = ry - 0.5 * staff_space * sign (diff) ; + } + + 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; + } + else + programming_error ("Tie is nowhere horizontal"); + + + + SCM controls = SCM_EOL; + for (int i= 4; i--;) + controls = gh_cons ( ly_offset2scm (b.control_[i]), controls); + return controls; } +MAKE_SCHEME_CALLBACK(Tie,set_spacing_rods); + +/* + TODO: set minimum distances for begin/end of line + */ +SCM +Tie::set_spacing_rods (SCM smob) +{ + Score_element*me = unsmob_element (smob); + Spanner*sp = dynamic_cast (me); + Rod r; + r.item_l_drul_ [LEFT]=sp->get_bound (LEFT); + r.item_l_drul_ [RIGHT]=sp->get_bound (RIGHT); + + r.distance_f_ + = gh_scm2double (me->get_elt_property ("minimum-length")) + * me->paper_l ()->get_var ("staffspace"); + r.add_to_cols (); + return SCM_UNSPECIFIED; +} -void -Tie::do_substitute_dependency(Score_elem*o, Score_elem*n) +MAKE_SCHEME_CALLBACK(Tie,brew_molecule); +SCM +Tie::brew_molecule (SCM smob) { - Note_head *new_l =n?(Note_head*)n->item():0; - if (o->item() == left_head_l_) - left_head_l_ = new_l; - else if (o->item() == right_head_l_) - right_head_l_ = new_l; + Score_element*me = unsmob_element (smob); + + SCM cp = me->get_elt_property ("control-points"); + if (cp == SCM_EOL) + { + cp = get_control_points (smob); + me->set_elt_property ("control-points", cp); + } + + Real thick = + gh_scm2double (me->get_elt_property ("thickness")) + * me->paper_l ()->get_var ("stafflinethickness"); + + Bezier b; + int i = 0; + for (SCM s= cp; s != SCM_EOL; s = gh_cdr (s)) + { + b.control_[i] = ly_scm2offset (gh_car (s)); + i++; + } + + Molecule a = me->lookup_l ()->slur (b, Directional_element_interface::get (me) * thick, thick); + + return a.create_scheme (); } -IMPLEMENT_STATIC_NAME(Tie); -IMPLEMENT_IS_TYPE_B1(Tie,Bow);