]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/tie.cc
* flower/include/interval.hh (struct Interval_t):
[lilypond.git] / lily / tie.cc
index baba2c7061a3f85909da74d99b389a37c9a7a307..4813ab197b97ef8658417287ba14e504722e7d0b 100644 (file)
 
   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 "tie.hh"
+
 #include <math.h>
 
+#include "spanner.hh"
 #include "lookup.hh"
-#include "paper-def.hh"
-#include "tie.hh"
-#include "note-head.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 (Direction d, Item * head_l)
+Tie::set_head (Grob *me, Direction d, Grob *h)
 {
-  assert (!head (d));
-  index_set_cell (get_elt_pointer ("heads"), d, head_l->self_scm_);
-  
-  set_bound (d, head_l);
-  add_dependency (head_l);
+  dynamic_cast<Spanner *> (me)->set_bound (d, h);
+  me->add_dependency (h);
 }
 
-Tie::Tie()
+Grob *
+Tie::head (Grob *me, Direction d)
 {
-  set_elt_pointer ("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;
-
+  Item *it = dynamic_cast<Spanner*> (me)->get_bound (d);
+  if (Note_head::has_interface (it))
+    return it;
+  else
+    return 0;
 }
 
-Note_head* 
-Tie::head (Direction d) const
+int
+Tie::get_column_rank (Grob *me, Direction d)
 {
-  SCM c = get_elt_pointer ("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 dynamic_cast<Note_head*> (unsmob_element (c));  
+  Grob *col = dynamic_cast<Item *> (h)->get_column ();
+  return Paper_column::get_rank (col);
 }
 
-Real
-Tie::position_f () const
+int
+Tie::get_position (Grob *me)
 {
-  return head (LEFT)
-    ? staff_symbol_referencer (head (LEFT)).position_f ()
-    : staff_symbol_referencer (head (RIGHT)).position_f () ;  
+  Direction d = head (me, LEFT) ? LEFT : RIGHT;
+  return (int) Staff_symbol_referencer::get_position (head (me, d));
 }
 
-
 /*
-  ugh: direction of the Tie is more complicated.  See [Ross] p136 and further
- */
+  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).
+
+  (what about linebreaks? )
+*/
 Direction
-Tie::get_default_dir () const
+Tie::get_default_dir (Grob *me)
 {
-  Stem * sl =  head(LEFT) ? head (LEFT)->stem_l () :0;
-  Stem * sr =  head(RIGHT) ? head (RIGHT)->stem_l () :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 (get_grob_direction (sl) == UP
+         && get_grob_direction (sr) == UP)
+       return DOWN;
+    }
+  else if (sl || sr)
+    {
+      Item *s = sl ? sl : sr;
+      return -get_grob_direction (s);
+    }
 
-  if (sl && directional_element (sl).get () == UP
-      && sr && directional_element (sr).get () == UP)
-    return DOWN;
-  else
-    return UP;
+  return UP;
 }
 
 void
-Tie::do_add_processing()
+Tie::set_direction (Grob *me)
 {
-  if (!(head (LEFT) && head (RIGHT)))
-    warning (_ ("lonely tie"));
+  if (!get_grob_direction (me))
+    {
+      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));
+    }
+}
 
+Interval
+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;
-  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);
-
-  index_set_cell (get_elt_pointer ("heads"), LEFT, new_head_drul[LEFT]->self_scm_ );
-  index_set_cell (get_elt_pointer ("heads"), RIGHT, new_head_drul[RIGHT]->self_scm_ );
-}
+  do
+    {
+      attachments[d]
+       = robust_relative_extent (me->get_bound (d),
+                                 common,
+                                 X_AXIS)[-d]
+       - gap * d;
+    }
+  while (flip (&d) != LEFT);
 
+  if (attachments.length () < 0.6 * staff_space)
+    {
+      /*
+       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);
+       }
+    }
+
+  return attachments;
+}  
+
+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);
+  
+  return attachments;
+}
+                       
 void
-Tie::after_line_breaking ()
+Tie::get_configuration (Grob *me_grob, Grob *common,
+                       Tie_configuration *conf,
+                       Drul_array< Array < Skyline_entry > > const *skylines,
+                       Tie_details const &details
+                       )
 {
-  if (!head (LEFT) && !head (RIGHT))
+  Spanner *me = dynamic_cast<Spanner*> (me_grob);
+  if (!head (me, LEFT) && !head (me, RIGHT))
     {
-      programming_error ("Tie without heads.");
-      set_elt_property ("transparent", SCM_BOOL_T);
-      set_extent_callback (0, X_AXIS);
-      set_extent_callback (0, Y_AXIS);
-      return;
+      programming_error ("tie without heads");
+      me->suicide ();
+      return ;
     }
 
-  if (!directional_element (this).get ())
-    directional_element (this).set (get_default_dir ());
+  Direction dir = CENTER;
   
-  Real staff_space = staff_symbol_referencer (this).staff_space ();
-  Real half_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");
+  int tie_position = (int) Tie::get_position (me);
+  int staff_position = (int) conf->position_;
 
-  /* 
-   Slur and tie placement [OSU]
-
-   Ties:
+  if (conf->dir_)
+    {
+      dir = conf->dir_;
+    }
+  else
+    {
+      dir = get_grob_direction (me);
+      if (!dir)
+       dir = get_default_dir (me);
+    }
 
-       * x = inner vertical tangent - d * gap
+  Real staff_space = details.staff_space_;
 
-   */
+  bool in_between = true;
+  Interval attachments = conf->attachment_x_;
+  Real gap = robust_scm2double (me->get_property ("x-gap"), 0.2);
+  if (attachments.is_empty())
+    {
+      if (!skylines)
+       attachments = get_default_attachments (me, common, gap,
+                                              &staff_position,
+                                              &in_between);
+      else
+       {
+         Real y = staff_space * 0.5 * staff_position;
+         attachments = get_skyline_attachment (*skylines, y);
+         attachments.widen (-gap);
+       }
+    }
 
+  Bezier b = slur_shape (attachments.length(),
+                        details.height_limit_,
+                        details.ratio_);
+  b.scale (1, dir);
+  
+  Offset middle = b.curve_point (0.5);
+  Offset edge = b.curve_point (0.0);
 
+  staff_position = int (rint (staff_position));
+  
+  Real dy = fabs (middle[Y_AXIS] - edge[Y_AXIS]);
+  bool in_space = !(Staff_symbol_referencer::on_staffline (me, (int) staff_position));
+  bool fits_in_space =
+    (dy < 0.6 * staff_space);
+  
   /*
-    OSU: not different for outer notes, so why all this code?
-    ie,  can we drop this, or should it be made switchable.
+    Avoid dot
    */
-  if (head (LEFT))
-    dx_f_drul_[LEFT] = head (LEFT)->extent (X_AXIS).length ();
-  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:
+  Grob *left_dot = unsmob_grob (me->get_bound (LEFT)->get_object ("dot"));
+  if (left_dot && in_space)
+    {
+      if (staff_position == Staff_symbol_referencer::get_position (left_dot))
+       {
+         staff_position += dir;
+         in_space = false;
+       }
+    }
 
-       * y = dx <  5ss: horizontal tangent
-        y = dx >= 5ss: y next interline - d * 0.25 ss
+  /*
+    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 = staff_position * staff_space * 0.5;
+      if (flag.extent (Y_AXIS).contains (y))
+       {
+         staff_position += dir;
+         in_space = !in_space;
+       }
+    }
 
-        which probably means that OSU assumes that
+  if (in_space != fits_in_space)
+    {
+      if (in_space)
+       {
+         staff_position += dir;
+       }
+      else
+       {
+         in_space = true;
+         staff_position += dir;
+       }
+    }
 
-           dy <= 5 dx
 
-        for smal slurs
+  /*
+    Putting larger in-space ties next to the notes forces
+    the edges to be opposite (Y-wise) to the tie direction.
    */
+  if (staff_position == tie_position
+      && in_space
+      && dy > 0.3 * staff_space)
+    {
+      staff_position += 2 * dir; 
+    }
 
-
-  Real ypos = position_f ();
-
-  Real y_f = half_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"))
+  if (!in_between
+      && in_space
+      && fabs (staff_position - tie_position) <= 1)
+    staff_position += 2*dir;
+  
+  
+  if (in_space)
     {
-      if (abs (ypos_i) % 2)
-       y_f += dir * half_space;
-      y_f += dir * y_gap_f;
+      if (fabs (dy) < 0.45 * staff_space)
+       {
+         /*
+           vertically center in space.
+         */
+         conf->attachment_x_ = attachments;
+         conf->center_tie_vertically(details);
+       }
+      else
+       {
+         conf->delta_y_ = 
+           dir * staff_space * (- 0.3);
+       }
     }
   else
     {
-      if (! (abs (ypos_i) % 2))
-       y_f += dir * half_space;
-      y_f += dir * half_space;
-      y_f -= dir * y_gap_f;
+      Real where = 0.5 * dir;
+      
+      Real rounding_dy = (where - middle[Y_AXIS]);
+      conf->delta_y_ = rounding_dy;
+
+      if (dir * b.curve_point (0.0)[Y_AXIS] <
+         dir * tie_position * 0.5 * staff_space)
+       conf->delta_y_ +=  staff_space * dir; 
     }
-  
-  dy_f_drul_[LEFT] = dy_f_drul_[RIGHT] = y_f;
-}
 
+  conf->dir_ = dir;
+  conf->position_ = staff_position;
 
+  if (skylines)
+    {
+      Real y = staff_space * 0.5 * staff_position;
+      attachments = get_skyline_attachment (*skylines, y);
+      attachments.widen (-gap);
+    }
+  conf->attachment_x_ = attachments;
+}
 
-Array<Rod>
-Tie::get_rods () const
+
+void
+Tie::set_default_control_points (Grob *me_grob)
 {
-  Array<Rod> a;
-  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]=get_bound (LEFT);
-  r.item_l_drul_ [RIGHT]=get_bound (RIGHT);  
+  int tie_position = (int) Tie::get_position (me);
+  conf.position_ = tie_position;
   
-  r.distance_f_ = paper_l ()->get_var ("tie_x_minimum");
-  a.push (r);
-  return a;
+  Tie_details details;
+  details.init (me);
+  get_configuration (me, common, &conf, 0, details);
+  set_control_points (me, common, conf, details);
 }
 
-
-
-
-Molecule 
-Tie::do_brew_molecule () const
+void
+Tie::set_control_points (Grob *me,
+                        Grob *common,
+                        Tie_configuration const &conf,
+                        Tie_details const &details
+                        )
 {
-  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);
+  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_
+                      ));
   
-  return a; 
+  SCM controls = SCM_EOL;
+  for (int i = 4; i--;)
+    controls = scm_cons (ly_offset2scm (b.control_[i]), controls);
+
+  me->set_property ("control-points", controls);
 }
 
 
 
-Bezier
-Tie::get_curve () const
+MAKE_SCHEME_CALLBACK (Tie, print, 1);
+SCM
+Tie::print (SCM smob)
 {
-  Direction d (directional_element (this).get ());
-  Bezier_bow b (get_encompass_offset_arr (), d);
+  Grob *me = unsmob_grob (smob);
 
-  Real staff_space = staff_symbol_referencer (this).staff_space ();
-  Real h_inf = paper_l ()->get_var ("tie_height_limit_factor") * staff_space;
-  Real r_0 = paper_l ()->get_var ("tie_ratio");
+  if (CENTER == get_grob_direction (me))
+    set_direction (me);
+      
+  SCM cp = me->get_property ("control-points");
+  if (!scm_is_pair (cp))
+    {
+      set_default_control_points (me);
+      cp = me->get_property ("control-points");
+    }
 
-  b.set_default_bezier (h_inf, r_0);
-  Bezier c = b.get_bezier ();
+  if (!scm_is_pair (cp))
+    return Stencil ().smobbed_copy ();
 
-  /* should do this for slurs as well. */
-  Array<Real> horizontal (c.solve_derivative (Offset (1,0)));
+  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;
 
-  if (horizontal.size ())
+  Bezier b;
+  int i = 0;
+  for (SCM s = cp; s != SCM_EOL; s = scm_cdr (s))
     {
-      /*
-       ugh. Doesnt work for non-horizontal curves.
-       */
-      Real y = c.curve_point (horizontal[0])[Y_AXIS];
-
-      Real ry = rint (y/staff_space) * staff_space;
-      Real diff = ry - y;
-      Real newy = y;
-      if (fabs (y) <= 2.0
-         && fabs (diff) < paper_l ()->get_var ("tie_staffline_clearance"))
-       {
-         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 - y0) / (y - y0)) + y0; 
+      b.control_[i] = ly_scm2offset (scm_car (s));
+      i++;
     }
+
+  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
-    programming_error ("Tie is nowhere horizontal");
-  return c;
-}
+    a = Lookup::slur (b,
+                     get_grob_direction (me) * staff_thick,
+                     thick);
 
-Array<Offset>
-Tie::get_encompass_offset_arr () const
-{
-  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;
+  return a.smobbed_copy ();
 }
 
-
+ADD_INTERFACE (Tie,
+              "tie-interface",
+              
+              "A tie connecting two noteheads.\n",
+              
+              "control-points "
+              "dash-fraction "
+              "dash-period "
+              "details "
+              "direction "
+              "thickness "
+              "x-gap ");