]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/tuplet-bracket.cc
* lily/tuplet-bracket.cc (calc_position_and_height): check musical
[lilypond.git] / lily / tuplet-bracket.cc
index bb397a27fa34cec23ade1098f6fcd23ac67a90c7..18e21a57df169a950532ec424632b43a1b5c2f94 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--2002 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c)  1997--2003 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 /*
 
 #include <math.h>
 
+#include "line-interface.hh"
 #include "beam.hh"
 #include "box.hh"
-#include "debug.hh"
+#include "warn.hh"
 #include "font-interface.hh"
 #include "molecule.hh"
 #include "paper-def.hh"
 static Grob*
 get_x_bound_grob (Grob *g, Direction my_dir)
 {
-  if (Note_column::stem_l (g)
+  if (Note_column::get_stem (g)
       && Note_column::dir (g) == my_dir)
     {
-      g = Note_column::stem_l (g);
+      g = Note_column::get_stem (g);
     }
   return g;
 }
@@ -55,26 +56,32 @@ get_x_bound_grob (Grob *g, Direction my_dir)
 
 
 Grob*
-Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> cols, bool *equally_long)
+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 *s1 = Note_column::get_stem (cols[0]); 
+  Grob *s2 = Note_column::get_stem (cols.top());    
 
-  Grob*b1 = s1 ? Stem::beam_l (s1) : 0;
-  Grob*b2 = s2 ? Stem::beam_l (s2) : 0;
+  Grob*b1 = s1 ? Stem::get_beam (s1) : 0;
+  Grob*b2 = s2 ? Stem::get_beam (s2) : 0;
   
   Spanner*sp = dynamic_cast<Spanner*> (me);  
 
   *equally_long= false;
-  if (! ( b1 && (b1 == b2) && !sp->broken_b() ))
+  if (! (b1 && (b1 == b2) && !sp->broken_b()))
       return 0;
 
   Link_array<Grob> beam_stems = Pointer_group_interface__extract_grobs
     (b1, (Grob*)0, "stems");
 
+  if (beam_stems.size() == 0)
+    {
+      programming_error ("Beam under tuplet bracket has no stems!");
+      *equally_long = 0;
+      return 0;
+    }
   
   *equally_long = (beam_stems[0] == s1 && beam_stems.top() == s2);
   return b1;
@@ -94,14 +101,27 @@ Tuplet_bracket::brew_molecule (SCM smob)
 {
   Grob *me= unsmob_grob (smob);
   Molecule  mol;
-  Link_array<Grob> column_arr=
+  Link_array<Grob> columns=
     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
-  if (!column_arr.size ())
+  if (!columns.size ())
     return mol.smobbed_copy ();
 
+  
+
+  {
+    SCM lp = me->get_grob_property ("left-position");
+    SCM rp = me->get_grob_property ("right-position");  
+
+    if (!gh_number_p (rp) || !gh_number_p (lp))
+      after_line_breaking (smob);
+  }
+  
+  Real ly = robust_scm2double (me->get_grob_property ("left-position"), 0);
+  Real ry = robust_scm2double (me->get_grob_property ("right-position"), 0);  
+  
   bool equally_long = false;
-  Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
+  Grob * par_beam = parallel_beam (me, columns, &equally_long);
 
   Spanner*sp = dynamic_cast<Spanner*> (me);  
 
@@ -127,24 +147,32 @@ Tuplet_bracket::brew_molecule (SCM smob)
   else if (numb == ly_symbol2scm ("if-no-beam"))
     number_visibility = !par_beam;
        
-  Grob * commonx = column_arr[0]->common_refpoint (column_arr.top (),X_AXIS);
-  Direction dir = Directional_element_interface::get (me);
+  Grob * commonx = columns[0]->common_refpoint (columns.top (),X_AXIS);
 
-  Grob * lgr = get_x_bound_grob (column_arr[0], dir);
-  Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
+  /*
+    Tuplet brackets are normally not broken, but we shouldn't crash if
+    they are.
+   */
+  commonx = commonx->common_refpoint (sp->get_bound(LEFT), X_AXIS);
+  commonx = commonx->common_refpoint (sp->get_bound(RIGHT), X_AXIS);  
+  
+  Direction dir = get_grob_direction (me);
+
+  Grob * lgr = get_x_bound_grob (columns[0], dir);
+  Grob * rgr = get_x_bound_grob (columns.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");
   
+  SCM number = me->get_grob_property ("text");
+
+  Paper_def *pap = me->get_paper ();
   if (gh_string_p (number) && number_visibility)
     {
       SCM properties = Font_interface::font_alist_chain (me);
-      Molecule num = Text_item::text2molecule (me, number, properties);
+      SCM snum = Text_item::interpret_markup (pap->self_scm (), properties, number);
+      Molecule num = *unsmob_molecule (snum);
       num.align_to (X_AXIS, CENTER);
       num.translate_axis (w/2, X_AXIS);
       num.align_to (Y_AXIS, CENTER);
@@ -153,27 +181,49 @@ Tuplet_bracket::brew_molecule (SCM smob)
 
       mol.add_molecule (num);
     }
-      
-  if (bracket_visibility)      
+
+
+  /*
+    No bracket when it would be smaller than the number.
+
+    TODO: should use GAP in calculation too.
+   */
+  if (bracket_visibility && number_visibility
+      && mol.extent (X_AXIS).length () > w)
     {
-      Real  lt =  me->paper_l ()->get_var ("linethickness");
+      bracket_visibility = false;
+    }
   
-      SCM thick = me->get_grob_property ("thickness");
-      if (gh_number_p (thick))
-       lt *= gh_scm2double (thick);
-      
+  if (bracket_visibility)      
+    {
       SCM gap = me->get_grob_property ("gap");
-
-      Real prot_size = 0.7;    // magic.
-
-      Molecule brack = make_bracket (Y_AXIS,
-                                    w, ry-ly, lt,
-                                    -prot_size*dir, -prot_size*dir,
+      
+      SCM fl = me->get_grob_property ("bracket-flare");
+      SCM eh = me->get_grob_property ("edge-height");
+      SCM sp = me->get_grob_property ("shorten-pair");
+      
+      Direction d = LEFT;
+      Drul_array<Real> height, flare, shorten;
+      do {
+       flare[d] =  height[d] = shorten[d] = 0.0;
+       if (is_number_pair (fl))
+         flare[d] +=  gh_scm2double (index_get_cell (fl, d));
+       if (is_number_pair (eh))
+         height[d] += gh_scm2double (index_get_cell (eh, d)) * - dir;
+       if (is_number_pair (sp))
+         shorten[d] +=  gh_scm2double (index_get_cell (sp, d));
+      }
+      while (flip (&d) != LEFT);
+      
+      Molecule brack = make_bracket (me, Y_AXIS,
+                                    Offset (w, ry - ly), 
+                                    height,
                                     gh_scm2double (gap),
-                                    0.0, 0.0);
+                                    flare,
+                                    shorten);
       mol.add_molecule (brack);
     }
-
+  
   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 ();
@@ -181,40 +231,50 @@ Tuplet_bracket::brew_molecule (SCM smob)
 
 /*
   should move to lookup?
+
+  TODO: this will fail for very short (shorter than the flare)
+  brackets.
  */
 Molecule
-Tuplet_bracket::make_bracket (Axis protusion_axis,
-                             Real dx, Real dy, Real thick, Real lprotrusion,
-                             Real rprotrusion, Real gap, Real left_widen,
-                             Real right_widen)
+Tuplet_bracket::make_bracket (Grob *me,        // for line properties.
+                             Axis protusion_axis,
+                             Offset dz,
+                             Drul_array<Real> height,
+                             Real gap,
+                             Drul_array<Real> flare,
+                             Drul_array<Real> shorten)
 {
-  Real len = Offset (dx,dy).length ();
-  Real gapx = dx*  (gap /  len);
-  Real gapy = dy*  (gap /  len);
-  Axis other = other_axis (protusion_axis);
-
-  Molecule l1 = Lookup::line (thick, Offset(0,0),
-                             Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
-  Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
-                             
-                             Offset (dx,dy));
-
-  Offset protusion;
-  protusion[other] = left_widen;
-  protusion[protusion_axis] = lprotrusion;
+  Drul_array<Offset> corners (Offset(0,0), dz);
+  
+  Real length = dz.length ();
+  Drul_array<Offset> gap_corners;
+
   
-  Molecule p1 = Lookup::line (thick, Offset(0,0), protusion);
+  Axis bracket_axis = other_axis (protusion_axis);
 
-  protusion[other] = right_widen;
-  protusion[protusion_axis] = rprotrusion;
-  Molecule p2 = Lookup::line (thick, Offset(dx,dy),Offset(dx,dy) + protusion);  
+  Drul_array<Offset> straight_corners = corners;
 
+  Direction d = LEFT;
+  do {
+    straight_corners[d] += - d * shorten[d] /length * dz;
+    gap_corners[d] = (dz * 0.5) + d * gap / length * dz;
+  } while (flip (&d) != LEFT);
+
+  Drul_array<Offset> flare_corners = straight_corners;
+  do {
+    flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
+    flare_corners[d][protusion_axis] += height[d];
+    straight_corners[d][bracket_axis] += - d * flare[d];
+  } while (flip (&d) != LEFT);
 
   Molecule m;
-  m.add_molecule (p1);
-  m.add_molecule (p2);
-  m.add_molecule (l1);
-  m.add_molecule (l2);
+  do {
+    m.add_molecule (Line_interface::line (me, straight_corners[d],
+                                                gap_corners[d]));
+    
+    m.add_molecule (Line_interface::line (me,  straight_corners[d],
+                                                flare_corners[d]));
+  } while (flip (&d) != LEFT);
 
   return m;  
 }
@@ -226,30 +286,52 @@ Tuplet_bracket::make_bracket (Axis protusion_axis,
 void
 Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy) 
 {
-  Link_array<Grob> column_arr=
+  Link_array<Grob> columns=
     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
 
-  Grob * commony = me->common_refpoint (me->get_grob_property ("note-columns"), Y_AXIS);
-  Grob * commonx = me->common_refpoint (me->get_grob_property ("note-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);  
+
+  Interval staff;
+
+  if (Grob * st = Staff_symbol_referencer::get_staff_symbol (me))
+    staff = st->extent (commony, Y_AXIS);
   
-  Direction dir = Directional_element_interface::get (me);
+  Direction dir = get_grob_direction (me);
 
   /*
     Use outer non-rest columns to determine slope
    */
   int l = 0;
-  while (l <column_arr.size () && Note_column::rest_b (column_arr[l]))
+  while (l <columns.size () && Note_column::rest_b (columns[l]))
     l ++;
 
-  int r = column_arr.size ()- 1;
-  while (r >= l && Note_column::rest_b (column_arr[r]))
+  int r = columns.size ()- 1;
+  while (r >= l && Note_column::rest_b (columns[r]))
     r--;
   
   if (l < r)
     {
-      *dy = column_arr[r]->extent (commony, Y_AXIS) [dir]
-       - column_arr[l]->extent (commony, Y_AXIS) [dir] ;
+      Interval rv =columns[r]->extent (commony, Y_AXIS);
+      Interval lv =columns[l]->extent (commony, Y_AXIS);
+      rv.unite (staff);
+      lv.unite (staff);
+      Real graphical_dy =  rv[dir] - lv[dir];
+
+      Slice ls = Note_column::head_positions_interval (columns[l]);
+      Slice rs = Note_column::head_positions_interval (columns[r]);
+      
+      Interval musical_dy;
+      musical_dy[UP] = rs[UP] - ls[UP];
+      musical_dy[DOWN] = rs[DOWN] - ls[DOWN];
+      if (sign (musical_dy[UP]) != sign (musical_dy[DOWN]))
+       *dy = 0.0;
+      else if (sign (graphical_dy) != sign (musical_dy[DOWN]))
+       *dy = 0.0;
+      else
+       *dy = graphical_dy;
     }
   else
     * dy = 0;
@@ -257,28 +339,29 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
 
   *offset = - dir * infinity_f;
 
-  if (!column_arr.size ())
+  if (!columns.size ())
     return;
 
 
   
-  Grob * lgr = get_x_bound_grob (column_arr[0], dir);
-  Grob * rgr = get_x_bound_grob (column_arr.top(), dir);  
+  Grob * lgr = get_x_bound_grob (columns[0], dir);
+  Grob * rgr = get_x_bound_grob (columns.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;
+  /*
+    Slope.
+  */
+  Real factor = columns.size () > 1 ? 1/ (x1 - x0) : 1.0;
   
-  for (int i = 0; i < column_arr.size ();  i++)
+  for (int i = 0; i < columns.size ();  i++)
     {
-      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;
+      Interval note_ext =columns[i]->extent (commony, Y_AXIS);
+      note_ext.unite (staff);
+      Real notey = note_ext[dir] - me->relative_coordinate (commony, Y_AXIS);
+      
+      Real x = columns[i]->relative_coordinate (commonx, X_AXIS) - x0;
       Real tuplety =  *dy * x * factor;
 
       if (notey * dir > (*offset + tuplety) * dir)
@@ -291,11 +374,12 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
   
   /*
     horizontal brackets should not collide with staff lines.
+    
    */
-  if (*dy == 0)
+  Real ss= Staff_symbol_referencer::staff_space (me);
+  if (*dy == 0 && fabs (*offset) <  ss * Staff_symbol_referencer::staff_radius (me))
     {
       // quantize, then do collision check.
-      Real ss= Staff_symbol_referencer::staff_space (me);
       *offset *= 2 / ss;
       
       *offset = rint (*offset);
@@ -307,22 +391,6 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
   
 }
 
-/*
-  use first -> last note for slope,
-*/
-void
-Tuplet_bracket::calc_dy (Grob*me,Real * dy)
-{
-  Link_array<Grob> column_arr=
-    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
-
-  /*
-    ugh. refps.
-   */
-  Direction d = Directional_element_interface::get (me);
-  *dy = column_arr.top ()->extent (column_arr.top (), Y_AXIS) [d]
-    - column_arr[0]->extent (column_arr[0], Y_AXIS) [d];
-}
 
 
 /*
@@ -333,14 +401,14 @@ SCM
 Tuplet_bracket::before_line_breaking (SCM smob)
 {
   Grob *me = unsmob_grob (smob);
-  Link_array<Grob> column_arr=
+  Link_array<Grob> columns=
     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
 
-  for (int i = column_arr.size(); i--;)
+  for (int i = columns.size(); i--;)
     {
-      Grob * s =Note_column::stem_l (column_arr[i]);
-      Grob * b = s ? Stem::beam_l (s): 0;
+      Grob * s =Note_column::get_stem (columns[i]);
+      Grob * b = s ? Stem::get_beam (s): 0;
       if (b)
        me->add_dependency (b);
     }
@@ -353,33 +421,37 @@ SCM
 Tuplet_bracket::after_line_breaking (SCM smob)
 {
   Grob * me = unsmob_grob (smob);
-  Link_array<Grob> column_arr=
+  Link_array<Grob> columns=
     Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
 
-  if (!column_arr.size ())
+  if (!columns.size ())
     {
       me->suicide ();
       return SCM_UNSPECIFIED;
     }
   if (dynamic_cast<Spanner*> (me)->broken_b ())
     {
-      me->warning ( "Tuplet_bracket was across linebreak. Farewell cruel world.");
+      me->warning (_("Killing tuplet bracket across linebreak."));
       me->suicide();
       return SCM_UNSPECIFIED;
     }
   
-  Direction dir = Directional_element_interface::get (me);
+  Direction dir = get_grob_direction (me);
   if (!dir)
     {
       dir = Tuplet_bracket::get_default_dir (me);
-      Directional_element_interface::set (me, dir);
+      set_grob_direction (me, dir);
     }
   
   bool equally_long = false;
-  Grob * par_beam = parallel_beam (me, column_arr, &equally_long);
+  Grob * par_beam = parallel_beam (me, columns, &equally_long);
 
+  /*
+    We follow the beam only if there is one, and we are next to it.
+   */
   Real dy, offset;
-  if (!par_beam)
+  if (!par_beam
+      || get_grob_direction (par_beam) != dir)
     {
       calc_position_and_height (me,&offset,&dy);
     }
@@ -451,16 +523,11 @@ Tuplet_bracket::add_column (Grob*me, Item*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.",
-  "note-columns padding gap left-position right-position bracket-visibility number-visibility thickness direction");
+  "note-columns bracket-flare edge-height shorten-pair padding gap left-position right-position bracket-visibility number-visibility thickness direction");