]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/tuplet-bracket.cc
trem fix
[lilypond.git] / lily / tuplet-bracket.cc
index da4f0585403be71e19c916ca22b204eb86e35294..2b6b6f7cb14bf7db0bee521e3d6055aae72ad969 100644 (file)
@@ -6,6 +6,22 @@
   (c)  1997--2002 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
+/*
+  TODO:
+
+  - tuplet bracket should probably be subject to the same rules as
+  beam sloping/quanting.
+
+  - There is no support for kneed brackets, or nested brackets.
+
+  - number placement for parallel beams should be much more advanced:
+    for sloped beams some extra horizontal offset must be introduced.
+
+  - number placement is usually done over the center note, not the
+    graphical center.
+  
+ */
+
 #include <math.h>
 
 #include "beam.hh"
 #include "debug.hh"
 #include "font-interface.hh"
 #include "molecule.hh"
-#include "paper-column.hh"
 #include "paper-def.hh"
 #include "text-item.hh"
 #include "tuplet-bracket.hh"
 #include "stem.hh"
 #include "note-column.hh"
-#include "dimensions.hh"
 #include "group-interface.hh"
 #include "directional-element-interface.hh"
 #include "spanner.hh"
 #include "staff-symbol-referencer.hh"
+#include "lookup.hh"
+
+
+static Grob*
+get_x_bound_grob (Grob *g, Direction my_dir)
+{
+  if (Note_column::stem_l (g)
+      && Note_column::dir (g) == my_dir)
+    {
+      g = Note_column::stem_l (g);
+    }
+  return g;
+}
+
+
+
+Grob*
+Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equally_long)
+{
+  /*
+    ugh: code dup. 
+  */
+  Grob *s1 = Note_column::stem_l (cols[0]); 
+  Grob *s2 = Note_column::stem_l (cols.top());    
+
+  Grob*b1 = s1 ? Stem::beam_l (s1) : 0;
+  Grob*b2 = s2 ? Stem::beam_l (s2) : 0;
+  
+  Spanner*sp = dynamic_cast<Spanner*> (me);  
+
+  *equally_long= false;
+  if (! ( b1 && (b1 == b2) && !sp->broken_b() ))
+      return 0;
+
+  Link_array<Grob> beam_stems = Pointer_group_interface__extract_grobs
+    (b1, (Grob*)0, "stems");
+
+  
+  *equally_long = (beam_stems[0] == s1 && beam_stems.top() == s2);
+  return b1;
+}
+
 
 /*
   TODO:
@@ -32,8 +88,6 @@
   follow beam precisely for determining tuplet number location.
   
  */
-
-
 MAKE_SCHEME_CALLBACK (Tuplet_bracket,brew_molecule,1);
 SCM
 Tuplet_bracket::brew_molecule (SCM smob) 
@@ -41,29 +95,23 @@ Tuplet_bracket::brew_molecule (SCM smob)
   Grob *me= unsmob_grob (smob);
   Molecule  mol;
   Link_array<Grob> column_arr=
-    Pointer_group_interface__extract_grobs (me, (Grob*)0, "columns");
-
+    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
   if (!column_arr.size ())
     return mol.smobbed_copy ();
 
+  bool equally_long = false;
+  Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
 
-  Grob *b1 = Note_column::stem_l (column_arr[0]); 
-  Grob *b2 = Note_column::stem_l (column_arr.top());    
-
-  b1 = b1 ? Stem::beam_l (b1) : 0;
-  b2 = b2 ? Stem::beam_l (b2) : 0;
-
-  
   Spanner*sp = dynamic_cast<Spanner*> (me);  
 
-  // Default behaviour: number always, bracket when no beam!
-  bool par_beam = b1 && (b1 == b2) && !sp->broken_b() ;
-  
-  bool bracket_visibility = !par_beam;
+  bool bracket_visibility = !(par_beam && equally_long);
   bool number_visibility = true;
 
-  SCM bracket = me->get_grob_property ("tuplet-bracket-visibility");
+  /*
+    Fixme: the type of this prop is sucky.
+   */
+  SCM bracket = me->get_grob_property ("bracket-visibility");
   if (gh_boolean_p (bracket))
     {
       bracket_visibility = gh_scm2bool (bracket);
@@ -71,21 +119,28 @@ Tuplet_bracket::brew_molecule (SCM smob)
   else if (bracket == ly_symbol2scm ("if-no-beam"))
     bracket_visibility = !par_beam;
 
-  SCM numb = me->get_grob_property ("tuplet-number-visibility");  
+  SCM numb = me->get_grob_property ("number-visibility");  
   if (gh_boolean_p (numb))
     {
       number_visibility = gh_scm2bool (numb);
     }
   else if (numb == ly_symbol2scm ("if-no-beam"))
     number_visibility = !par_beam;
-  
        
-  Real ncw = column_arr.top ()->extent (column_arr.top (), X_AXIS).length ();
-  Real w = sp->spanner_length () + ncw;
-
+  Grob * commonx = column_arr[0]->common_refpoint (column_arr.top (),X_AXIS);
   Direction dir = Directional_element_interface::get (me);
-  Real dy = gh_scm2double (me->get_grob_property ("delta-y"));
+
+  Grob * lgr = get_x_bound_grob (column_arr[0], dir);
+  Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
+  Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
+  Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
+
+  Real w = x1 -x0;
+
+  Real ly = gh_scm2double (me->get_grob_property ("left-position"));
+  Real ry = gh_scm2double (me->get_grob_property ("right-position"));  
   SCM number = me->get_grob_property ("text");
+  
   if (gh_string_p (number) && number_visibility)
     {
       SCM properties = Font_interface::font_alist_chain (me);
@@ -94,35 +149,96 @@ Tuplet_bracket::brew_molecule (SCM smob)
       num.translate_axis (w/2, X_AXIS);
       num.align_to (Y_AXIS, CENTER);
        
-      num.translate_axis (dy/2, Y_AXIS);
+      num.translate_axis ((ry-ly)/2, Y_AXIS);
 
       mol.add_molecule (num);
     }
       
   if (bracket_visibility)      
     {
-      Real  lt =  me->paper_l ()->get_var ("stafflinethickness");
-         
-      SCM thick = me->get_grob_property ("thick");
-      SCM gap = me->get_grob_property ("number-gap");
-         
-      SCM at =scm_list_n (ly_symbol2scm ("tuplet"),
-                      gh_double2scm (1.0),
-                      gap,
-                      gh_double2scm (w),
-                      gh_double2scm (dy),
-                      gh_double2scm (gh_scm2double (thick)* lt),
-                      gh_int2scm (dir),
-                      SCM_UNDEFINED);
-
-      Box b;
-      mol.add_molecule (Molecule (b, at));
+      Real  lt =  me->paper_l ()->get_var ("linethickness");
+  
+      SCM thick = me->get_grob_property ("thickness");
+      if (gh_number_p (thick))
+       lt *= gh_scm2double (thick);
+      
+      SCM gap = me->get_grob_property ("gap");
+      SCM ew = me->get_grob_property ("edge-width");
+      SCM eh = me->get_grob_property ("edge-height");
+      SCM sp = me->get_grob_property ("shorten-pair");
+      
+      Direction d = LEFT;
+      Drul_array<Real> height, width, shorten;
+      do {
+       width[d] =  height[d] = shorten[d] = 0.0;
+       if ( gh_pair_p (ew) )
+         width[d] +=  gh_scm2double (index_cell (ew, d)) * d;
+       if ( gh_pair_p (eh) )
+         height[d] += gh_scm2double (index_cell (eh, d));
+       if ( gh_pair_p (sp) )
+         shorten[d] +=  gh_scm2double (index_cell (sp, d));
+      }
+      while (flip (&d) != LEFT);
+      
+      Molecule brack = make_bracket (Y_AXIS,
+                                    w, ry - ly, lt,
+                                    -height[LEFT]*dir, -height[RIGHT]*dir,
+                                    gh_scm2double (gap),
+                                    width[LEFT], width[RIGHT],
+                                    shorten[LEFT], shorten[RIGHT]);
+      mol.add_molecule (brack);
+      mol.translate_axis (dir * max(height[LEFT], height[RIGHT]), Y_AXIS);
     }
 
+  mol.translate_axis (ly, Y_AXIS);
+  mol.translate_axis (x0  - sp->get_bound (LEFT)->relative_coordinate (commonx,X_AXIS),X_AXIS);
   return mol.smobbed_copy ();
 }
 
-
+/*
+  should move to lookup?
+ */
+Molecule
+Tuplet_bracket::make_bracket (Axis protusion_axis,
+                             Real dx, Real dy, Real thick, Real left_height,
+                             Real right_height, Real gap, Real left_widen,
+                             Real right_widen, Real left_shorten, Real right_shorten)
+{
+  Real len = Offset (dx,dy).length ();
+  Real gapx = dx * (gap /  len);
+  Real gapy = dy * (gap /  len);
+  Real lshortx = dx * (left_shorten /  len);
+  Real lshorty = dy * (left_shorten /  len);
+  Real rshortx = dx * (right_shorten /  len);
+  Real rshorty = dy * (right_shorten /  len);
+  Axis other = other_axis (protusion_axis);
+  
+  Molecule l1 = Lookup::line (thick, Offset(lshortx, lshorty),
+                             Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
+
+  Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
+                             Offset (dx - rshortx, dy - rshorty));
+
+  Offset protusion;
+  protusion[other] = -left_widen;
+  protusion[protusion_axis] = left_height;
+  Molecule p1 = Lookup::line (thick, 
+                             Offset(lshortx, lshorty), 
+                             Offset(lshortx, lshorty) + protusion);
+  protusion[other] = right_widen;
+  protusion[protusion_axis] = right_height;
+  Molecule p2 = Lookup::line (thick, 
+                             Offset(dx - rshortx, dy - rshorty), 
+                             Offset(dx - rshortx, dy - rshorty) + protusion);  
+
+  Molecule m;
+  m.add_molecule (p1);
+  m.add_molecule (p2);
+  m.add_molecule (l1);
+  m.add_molecule (l2);
+
+  return m;  
+}
 
 
 /*
@@ -132,13 +248,14 @@ void
 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy) 
 {
   Link_array<Grob> column_arr=
-    Pointer_group_interface__extract_grobs (me, (Grob*)0, "columns");
+    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
 
-  Grob * commony = me->common_refpoint (me->get_grob_property ("columns"), Y_AXIS);
-  Grob * commonx = me->common_refpoint (me->get_grob_property ("columns"), X_AXIS);  
+  SCM cols = me->get_grob_property ("note-columns");
+  Grob * commony = common_refpoint_of_list (cols, me, Y_AXIS);
+  Grob * commonx = common_refpoint_of_list (cols, me, X_AXIS);  
   
-  Direction d = Directional_element_interface::get (me);
+  Direction dir = Directional_element_interface::get (me);
 
   /*
     Use outer non-rest columns to determine slope
@@ -153,43 +270,49 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
   
   if (l < r)
     {
-      *dy = column_arr[r]->extent (commony, Y_AXIS) [d]
-       - column_arr[l]->extent (commony, Y_AXIS) [d] ;
+      *dy = column_arr[r]->extent (commony, Y_AXIS) [dir]
+       - column_arr[l]->extent (commony, Y_AXIS) [dir] ;
     }
   else
     * dy = 0;
 
 
-  *offset = - d * infinity_f;
+  *offset = - dir * infinity_f;
 
   if (!column_arr.size ())
     return;
+
+
   
-  Real x0 = column_arr[0]->relative_coordinate (commonx, X_AXIS);
-  Real x1 = column_arr.top ()->relative_coordinate (commonx, X_AXIS);
-  
+  Grob * lgr = get_x_bound_grob (column_arr[0], dir);
+  Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
+  Real x0 = lgr->extent (commonx,X_AXIS)[LEFT];
+  Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
+
+
+    /*
+      Slope.
+    */
   Real factor = column_arr.size () > 1 ? 1/ (x1 - x0) : 1.0;
   
   for (int i = 0; i < column_arr.size ();  i++)
     {
-      Real notey = column_arr[i]->extent (commony, Y_AXIS)[d] 
+      Real notey = column_arr[i]->extent (commony, Y_AXIS)[dir
        - me->relative_coordinate (commony, Y_AXIS);
 
       Real x = column_arr[i]->relative_coordinate (commonx, X_AXIS) - x0;
       Real tuplety =  *dy * x * factor;
 
-      if (notey * d > (*offset + tuplety) * d)
+      if (notey * dir > (*offset + tuplety) * dir)
        *offset = notey - tuplety; 
     }
 
   // padding
-  *offset +=  1.0 *d;
+  *offset +=  gh_scm2double (me->get_grob_property ("padding")) *dir;
 
   
   /*
     horizontal brackets should not collide with staff lines.
-
-    
    */
   if (*dy == 0)
     {
@@ -199,7 +322,7 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
       
       *offset = rint (*offset);
       if (Staff_symbol_referencer::on_staffline (me, (int) rint (*offset)))
-       *offset += d;
+       *offset += dir;
 
       *offset *= 0.5 * ss;
     }
@@ -213,7 +336,7 @@ void
 Tuplet_bracket::calc_dy (Grob*me,Real * dy)
 {
   Link_array<Grob> column_arr=
-    Pointer_group_interface__extract_grobs (me, (Grob*)0, "columns");
+    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
   /*
     ugh. refps.
@@ -222,54 +345,117 @@ Tuplet_bracket::calc_dy (Grob*me,Real * dy)
   *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
     - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
 }
+
+
+/*
+  We depend on the beams if there are any.
+ */
+MAKE_SCHEME_CALLBACK (Tuplet_bracket,before_line_breaking,1);
+SCM
+Tuplet_bracket::before_line_breaking (SCM smob)
+{
+  Grob *me = unsmob_grob (smob);
+  Link_array<Grob> column_arr=
+    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
+
+
+  for (int i = column_arr.size(); i--;)
+    {
+      Grob * s =Note_column::stem_l (column_arr[i]);
+      Grob * b = s ? Stem::beam_l (s): 0;
+      if (b)
+       me->add_dependency (b);
+    }
+  return SCM_UNDEFINED;
+}
+
 MAKE_SCHEME_CALLBACK (Tuplet_bracket,after_line_breaking,1);
 
 SCM
 Tuplet_bracket::after_line_breaking (SCM smob)
 {
   Grob * me = unsmob_grob (smob);
-  Link_array<Note_column> column_arr=
-    Pointer_group_interface__extract_grobs (me, (Note_column*)0, "columns");
+  Link_array<Grob> column_arr=
+    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
   if (!column_arr.size ())
     {
       me->suicide ();
       return SCM_UNSPECIFIED;
     }
-
-  Direction d = Directional_element_interface::get (me);
-  if (!d)
+  if (dynamic_cast<Spanner*> (me)->broken_b ())
     {
-      d = Tuplet_bracket::get_default_dir (me);
-      Directional_element_interface::set (me, d);
-
+      me->warning ( "Tuplet_bracket was across linebreak. Farewell cruel world.");
+      me->suicide();
+      return SCM_UNSPECIFIED;
+    }
+  
+  Direction dir = Directional_element_interface::get (me);
+  if (!dir)
+    {
+      dir = Tuplet_bracket::get_default_dir (me);
+      Directional_element_interface::set (me, dir);
     }
+  
+  bool equally_long = false;
+  Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
+
+  /*
+    We follow the beam only if there is one, and we are next to it.
+   */
   Real dy, offset;
+  if (!par_beam
+      || Directional_element_interface::get (par_beam) != dir)
+    {
+      calc_position_and_height (me,&offset,&dy);
+    }
+  else
+    {
+      SCM ps =  par_beam->get_grob_property ("positions"); 
 
-  calc_position_and_height (me,&offset,&dy);
+      Real lp = gh_scm2double (gh_car (ps));
+      Real rp = gh_scm2double (gh_cdr (ps));
 
-  if (!gh_number_p (me->get_grob_property ("delta-y")))
-    me->set_grob_property ("delta-y", gh_double2scm (dy));
+      /*
+       duh. magic.
+       */
+      offset = lp + dir * (0.5 + gh_scm2double (me->get_grob_property ("padding")));
+      dy = rp- lp;
+    }
+  
+  
+  SCM lp =  me->get_grob_property ("left-position");
+  SCM rp = me->get_grob_property ("right-position");  
+  
+  if (gh_number_p (lp) && !gh_number_p (rp))
+    {
+      rp = gh_double2scm (gh_scm2double (lp) + dy);
+    }
+  else if (gh_number_p (rp) && !gh_number_p (lp))
+    {
+      lp = gh_double2scm (gh_scm2double (rp) - dy);
+    }
+  else if (!gh_number_p (rp) && !gh_number_p (lp))
+    {
+      lp = gh_double2scm (offset);
+      rp = gh_double2scm (offset +dy);
+    }
+
+  me->set_grob_property ("left-position", lp);
+  me->set_grob_property ("right-position", rp);
 
-  me->translate_axis (offset, Y_AXIS);
   return SCM_UNSPECIFIED;
 }
 
 
+/*
+  similar to slur.
+ */
 Direction
 Tuplet_bracket::get_default_dir (Grob*me)
 {
   Direction d = UP;
-  SCM dir_sym =me->get_grob_property ("dir-forced");
-  if (ly_dir_p (dir_sym))
-    {
-      d= to_dir (dir_sym);
-      if (d != CENTER)
-       return d;
-    }
-
-  d = UP ;
-  for (SCM s = me->get_grob_property ("columns"); gh_pair_p (s); s = ly_cdr (s))
+  for (SCM s = me->get_grob_property ("note-columns"); gh_pair_p (s); s = ly_cdr (s))
     {
       Grob * nc = unsmob_grob (ly_car (s));
       if (Note_column::dir (nc) < 0) 
@@ -278,30 +464,24 @@ Tuplet_bracket::get_default_dir (Grob*me)
          break;
        }
     }
-  
   return d;
 }
 
 void
 Tuplet_bracket::add_column (Grob*me, Item*n)
 {
-  Pointer_group_interface::add_grob (me, ly_symbol2scm ("columns"), n);
+  Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-columns"), n);
   me->add_dependency (n);
 
   add_bound_item (dynamic_cast<Spanner*> (me), n);
 }
 
 
-bool
-Tuplet_bracket::has_interface (Grob*me)
-{
-  return me->has_interface (ly_symbol2scm ("tuplet-bracket-interface"));
-}
 
 
 
 
 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
   "A bracket with a number in the middle, used for tuplets.",
-  "columns number-gap delta-y tuplet-bracket-visibility tuplet-number-visibility thick direction");
+  "note-columns edge-width edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");