]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/tie.cc
release: 1.3.88
[lilypond.git] / lily / tie.cc
index 6eb820b71f5819f68526f0d82b001c259b00e5a8..d40b0c33694dfcbb9a23f4dc6429820759a113aa 100644 (file)
@@ -7,10 +7,11 @@
 */
 #include <math.h>
 
+#include "spanner.hh"
 #include "lookup.hh"
 #include "paper-def.hh"
 #include "tie.hh"
-#include "note-head.hh"
+#include "rhythmic-head.hh"
 #include "bezier.hh"
 #include "paper-column.hh"
 #include "debug.hh"
 #include "molecule.hh"
 #include "bezier-bow.hh"
 #include "stem.hh"
+#include "note-head.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 (Direction d, Item * head_l)
+Tie::set_head (Score_element*me,Direction d, Item * head_l)
 {
-  assert (!head (d));
-  index_set_cell (get_elt_property ("heads"), d, head_l->self_scm_);
+  assert (!head (me,d));
+  index_set_cell (me->get_elt_property ("heads"), d, head_l->self_scm ());
   
-  set_bounds (d, head_l);
-  add_dependency (head_l);
+  dynamic_cast<Spanner*> (me)->set_bound (d, head_l);
+  me->add_dependency (head_l);
 }
 
-Tie::Tie()
+void
+Tie::set_interface (Score_element*me)
 {
-  set_elt_property ("heads", gh_cons (SCM_EOL, SCM_EOL));
-  dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = 0.0;
-  dx_f_drul_[LEFT] = dx_f_drul_[RIGHT] = 0.0;
-
+  me->set_elt_property ("heads", gh_cons (SCM_EOL, SCM_EOL));
+  me->set_interface (ly_symbol2scm ("tie-interface"));
 }
 
-Note_head* 
-Tie::head (Direction d) const
+bool
+Tie::has_interface (Score_element*me)
 {
-  SCM c = get_elt_property ("heads");
-  c = index_cell (c, d);
-
-  return dynamic_cast<Note_head*> (unsmob_element (c));  
+  return me->has_interface (ly_symbol2scm ("tie-interface"));
 }
 
-
-/*
-  ugh: direction of the Tie is more complicated.  See [Ross] p136 and further
- */
-Direction
-Tie::get_default_dir () const
+Score_element*
+Tie::head (Score_element*me, Direction d) 
 {
-  Stem * sl =  head(LEFT) ? head (LEFT)->stem_l () :0;
-  Stem * sr =  head(RIGHT) ? head (RIGHT)->stem_l () :0;  
+  SCM c = me->get_elt_property ("heads");
+  c = index_cell (c, d);
 
-  if (sl && directional_element (sl).get () == UP
-      && sr && directional_element (sr).get () == UP)
-    return DOWN;
-  else
-    return UP;
+  return unsmob_element (c);
 }
 
-void
-Tie::do_add_processing()
+Real
+Tie::position_f (Score_element*me) 
 {
-  if (!(head (LEFT) && head (RIGHT)))
-    warning (_ ("lonely tie"));
-
-  Direction d = LEFT;
-  Drul_array<Note_head *> new_head_drul;
-  new_head_drul[LEFT] = head(LEFT);
-  new_head_drul[RIGHT] = head(RIGHT);  
-  do {
-    if (!head (d))
-      new_head_drul[d] = head((Direction)-d);
-  } while (flip(&d) != LEFT);
+  Direction d = head (me,LEFT) ? LEFT:RIGHT;
+  return Staff_symbol_referencer::position_f (head (me,d));
+}
 
-  index_set_cell (get_elt_property ("heads"), LEFT, new_head_drul[LEFT]->self_scm_ );
-  index_set_cell (get_elt_property ("heads"), RIGHT, new_head_drul[LEFT]->self_scm_ );
 
-}
+/*
+  Default:  Put the tie oppositie of the stem [Wanske p231]
 
-void
-Tie::do_post_processing()
+  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) 
 {
-  if (!head (LEFT) && !head (RIGHT))
+  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)
     {
-      programming_error ("Tie without heads.");
-      set_elt_property ("transparent", SCM_BOOL_T);
-      set_empty (X_AXIS);
-      set_empty (Y_AXIS);
-      return;
+      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);
     }
 
-  if (!directional_element (this).get ())
-    directional_element (this).set (get_default_dir ());
   
-  Real staff_space = paper_l ()->get_var ("interline");
-  Real half_staff_space = staff_space / 2;
-  Real x_gap_f = paper_l ()->get_var ("tie_x_gap");
-  Real y_gap_f = paper_l ()->get_var ("tie_y_gap");
-
-  /* 
-   Slur and tie placement [OSU]
-
-   Ties:
-
-       * x = inner vertical tangent - d * gap
-
-   */
+  return UP;
+}
 
 
-  /*
-    OSU: not different for outer notes, so why all this code?
-    ie,  can we drop this, or should it be made switchable.
-   */
-  if (head (LEFT))
-    dx_f_drul_[LEFT] = head (LEFT)->extent (X_AXIS).length ();
+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
-    dx_f_drul_[LEFT] = get_broken_left_end_align ();
-  dx_f_drul_[LEFT] += x_gap_f;
-  dx_f_drul_[RIGHT] -= x_gap_f;
-
-  /* 
-   Slur and tie placement [OSU]  -- check this
-
-   Ties:
-
-       * y = dx <  5ss: horizontal tangent
-        y = dx >= 5ss: y next interline - d * 0.25 ss
-
-        which probably means that OSU assumes that
-
-           dy <= 5 dx
+    {
+      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);
 
-        for smal slurs
-   */
+  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 ypos = head (LEFT)
-    ? staff_symbol_referencer (head (LEFT)).position_f ()
-    : staff_symbol_referencer (head (RIGHT)).position_f () ;  
+  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 y_f = half_staff_space * ypos; 
-  int ypos_i = int (ypos);
-  Real dx_f = extent (X_AXIS).length () + dx_f_drul_[RIGHT] - dx_f_drul_[LEFT];
-  Direction dir = directional_element (this).get();
-  if (dx_f < paper_l ()->get_var ("tie_staffspace_length"))
+  Real width;
+  if (Note_head::has_interface (me->get_bound (LEFT))
+      && Note_head::has_interface (me->get_bound (RIGHT)))
     {
-      if (abs (ypos_i) % 2)
-       y_f += dir * half_staff_space;
-      y_f += dir * y_gap_f;
+      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 (! (abs (ypos_i) % 2))
-       y_f += dir * half_staff_space;
-      y_f += dir * half_staff_space;
-      y_f -= dir * y_gap_f;
+      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;
     }
   
-  dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = y_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");
 
 
-Array<Rod>
-Tie::get_rods () const
-{
-  Array<Rod> a;
-  Rod r;
-  r.item_l_drul_ = spanned_drul_;
-  r.distance_f_ = paper_l ()->get_var ("tie_x_minimum");
-  a.push (r);
-  return a;
-}
+  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?
 
-Molecule*
-Tie::do_brew_molecule_p () const
-{
-  Real thick = paper_l ()->get_var ("tie_thickness");
-  Bezier one = get_curve ();
-
-  Molecule a;
-  SCM d =  get_elt_property ("dashed");
-  if (gh_number_p (d))
-    a = lookup_l ()->dashed_slur (one, thick, gh_scm2int (d));
-  else
-    a = lookup_l ()->slur (one, directional_element (this).get () * thick, thick);
-  
-  return new Molecule (a); 
-}
+    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;
 
-Bezier
-Tie::get_curve () const
-{
-  Direction d (directional_element (this).get ());
-  Bezier_bow b (get_encompass_offset_arr (), d);
+  /*
+    todo: prevent ending / staffline collision.
 
-  b.ratio_ = paper_l ()->get_var ("slur_ratio");
-  b.height_limit_ = paper_l ()->get_var ("slur_height_limit");
-  b.rc_factor_ = paper_l ()->get_var ("slur_rc_factor");
+    todo: tie / stem collision
+   */
 
-  b.calculate ();
-  Bezier c (b.get_curve ());
+  b = slur_shape(width,h_inf, r_0);
+  b.scale (1, dir);
+  b.translate (Offset (left_x, ypos));
+  
 
-  /* should do this for slurs as well. */
-  Array<Real> horizontal (c.solve_derivative (Offset (1,0)));
+  /*
+    Avoid colliding of the horizontal part with stafflines.
+    
+    should do me for slurs as well.
 
+   */
+  Array<Real> horizontal (b.solve_derivative (Offset (1,0)));
   if (horizontal.size ())
     {
       /*
        ugh. Doesnt work for non-horizontal curves.
        */
-      Real space = staff_symbol_referencer (this).staff_space ();
-      Real y = c.curve_point (horizontal[0])[Y_AXIS];
+      Real y = b.curve_point (horizontal[0])[Y_AXIS];
 
-      Real ry = rint (y/space) * space;
+      Real ry = rint (y/staff_space) * staff_space;
       Real diff = ry - y;
       Real newy = y;
-      if (fabs (diff) < paper_l ()->get_var ("tie_staffline_clearance"))
+      if (fabs (y) <= Staff_symbol_referencer::staff_radius (me)
+         && fabs (diff) < me->paper_l ()->get_var ("tie_staffline_clearance"))
        {
-         newy = ry - 0.5 * space * sign (diff) ;
+         newy = ry - 0.5 * staff_space * sign (diff) ;
        }
 
-      Real y0 = c.control_ [0][Y_AXIS];
-      c.control_[2][Y_AXIS] = 
-      c.control_[1][Y_AXIS] =
-       (c.control_[1][Y_AXIS] - y0)  * (newy / y) + y0; 
+      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");
-  return c;
+
+
+
+  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<Spanner*> (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;
+}
 
-Array<Offset>
-Tie::get_encompass_offset_arr () const
+MAKE_SCHEME_CALLBACK(Tie,brew_molecule);
+SCM
+Tie::brew_molecule (SCM smob) 
 {
-  Array<Offset> offset_arr;
-  offset_arr.push (Offset (dx_f_drul_[LEFT], dy_f_drul_[LEFT]));
-  offset_arr.push (Offset (spanner_length () + dx_f_drul_[RIGHT],
-                          dy_f_drul_[RIGHT]));
-                     
-  return offset_arr;
+  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 ();
 }