]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/tuplet-bracket.cc
2003 -> 2004
[lilypond.git] / lily / tuplet-bracket.cc
index 1d6a7fe12fa82b0b00864f72f7879083d11c785a..221d323a6cc2cb97b740a7a1f31f627cc134ba85 100644 (file)
@@ -1,9 +1,10 @@
 /*
-  plet-spanner.cc -- implement Tuplet_bracket
+  tuplet-bracket.cc -- implement Tuplet_bracket
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--2003 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1997--2004 Jan Nieuwenhuizen <janneke@gnu.org>
+  Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 /*
@@ -24,6 +25,7 @@
 
 #include <math.h>
 
+#include "line-interface.hh"
 #include "beam.hh"
 #include "box.hh"
 #include "warn.hh"
@@ -69,7 +71,7 @@ Tuplet_bracket::parallel_beam (Grob *me, Link_array<Grob> const &cols, bool *equ
   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
@@ -106,6 +108,19 @@ Tuplet_bracket::brew_molecule (SCM smob)
   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, columns, &equally_long);
 
@@ -142,7 +157,7 @@ Tuplet_bracket::brew_molecule (SCM smob)
   commonx = commonx->common_refpoint (sp->get_bound(LEFT), X_AXIS);
   commonx = commonx->common_refpoint (sp->get_bound(RIGHT), X_AXIS);  
   
-  Direction dir = Directional_element_interface::get (me);
+  Direction dir = get_grob_direction (me);
 
   Grob * lgr = get_x_bound_grob (columns[0], dir);
   Grob * rgr = get_x_bound_grob (columns.top(), dir);  
@@ -150,15 +165,15 @@ Tuplet_bracket::brew_molecule (SCM smob)
   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);
@@ -167,39 +182,46 @@ 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->get_paper ()->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)      
+    {
+      Real ss =   Staff_symbol_referencer::staff_space (me);
+      Real gap = robust_scm2double (me->get_grob_property ("gap"), 1.0)
+       * ss;
       
-      SCM gap = me->get_grob_property ("gap");
-      SCM ew = me->get_grob_property ("edge-widen");
+      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, width, shorten;
+      Drul_array<Real> height, flare, shorten;
       do {
-       width[d] =  height[d] = shorten[d] = 0.0;
-       if ( ly_number_pair_p (ew) )
-         width[d] +=  gh_scm2double (index_get_cell (ew, d));
-       if ( ly_number_pair_p (eh) )
-         height[d] += gh_scm2double (index_get_cell (eh, d)) * - dir;
-       if ( ly_number_pair_p (sp) )
-         shorten[d] +=  gh_scm2double (index_get_cell (sp, d));
+       flare[d] =  height[d] = shorten[d] = 0.0;
+       if (is_number_pair (fl))
+         flare[d] +=  ss * gh_scm2double (index_get_cell (fl, d));
+       if (is_number_pair (eh))
+         height[d] +=  - dir * ss *gh_scm2double (index_get_cell (eh, d));
+       if (is_number_pair (sp))
+         shorten[d] +=  ss *gh_scm2double (index_get_cell (sp, d));
       }
       while (flip (&d) != LEFT);
       
-      Molecule brack = make_bracket (Y_AXIS,
-                                    w, ry - ly, lt,
-                                    height,
-                                    gh_scm2double (gap),
-                                    width,
-                                    shorten);
+      Molecule brack = make_bracket (me, Y_AXIS,
+                                    Offset (w, ry - ly), 
+                                    height, gap,
+                                    flare, shorten);
       mol.add_molecule (brack);
     }
   
@@ -210,49 +232,59 @@ 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, Drul_array<Real> height,
+Tuplet_bracket::make_bracket (Grob *me,        // for line properties.
+                             Axis protusion_axis,
+                             Offset dz,
+                             Drul_array<Real> height,
                              Real gap,
-                             Drul_array<Real> widen,
+                             Drul_array<Real> flare,
                              Drul_array<Real> shorten)
 {
-  Real len = Offset (dx,dy).length ();
-  Real gapx = dx * (gap /  len);
-  Real gapy = dy * (gap /  len);
-  Drul_array<Real> shortx, shorty;
+  Drul_array<Offset> corners (Offset(0,0), dz);
+  
+  Real length = dz.length ();
+  Drul_array<Offset> gap_corners;
+
+  
+  Axis bracket_axis = other_axis (protusion_axis);
+
+  Drul_array<Offset> straight_corners = corners;
+
   Direction d = LEFT;
   do {
-    shortx[d] = dx * (shorten[d] /  len);
-    shorty[d] = dy * (shorten[d] /  len);
-  }
-  while (flip (&d) != LEFT);
-  Axis other = other_axis (protusion_axis);
+    straight_corners[d] += - d * shorten[d] /length * dz;
+  } while (flip (&d) != LEFT);
+
+  /*
+    UGH: the shortening factor is magic.
+   */
+  gap = gap <?
+    (0.66 * (straight_corners[RIGHT] - straight_corners[LEFT]).length ());
   
-  Molecule l1 = Lookup::line (thick, Offset(shortx[LEFT], shorty[LEFT]),
-                             Offset ( (dx - gapx)/2, (dy - gapy)/2 ));
-
-  Molecule l2 = Lookup::line (thick, Offset((dx + gapx) / 2,(dy + gapy) / 2),
-                             Offset (dx - shortx[RIGHT], dy - shorty[RIGHT]));
-
-  Offset protusion;
-  protusion[other] = -widen[LEFT];
-  protusion[protusion_axis] = height[LEFT];
-  Molecule p1 = Lookup::line (thick, 
-                             Offset(shortx[LEFT], shorty[LEFT]), 
-                             Offset(shortx[LEFT], shorty[LEFT]) + protusion);
-  protusion[other] = widen[RIGHT];
-  protusion[protusion_axis] = height[RIGHT];
-  Molecule p2 = Lookup::line (thick, 
-                             Offset(dx - shortx[RIGHT], dy - shorty[RIGHT]), 
-                             Offset(dx - shortx[RIGHT], dy - shorty[RIGHT]) + protusion);  
+  do {
+    gap_corners[d] = (dz * 0.5) + d * 0.5 * 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;  
 }
@@ -271,8 +303,12 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
   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
@@ -287,8 +323,24 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
   
   if (l < r)
     {
-      *dy = columns[r]->extent (commony, Y_AXIS) [dir]
-       - columns[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;
@@ -307,15 +359,16 @@ Tuplet_bracket::calc_position_and_height (Grob*me,Real *offset, Real * dy)
   Real x1 = rgr->extent (commonx,X_AXIS)[RIGHT];
 
 
-    /*
-      Slope.
-    */
+  /*
+    Slope.
+  */
   Real factor = columns.size () > 1 ? 1/ (x1 - x0) : 1.0;
   
   for (int i = 0; i < columns.size ();  i++)
     {
-      Real notey = columns[i]->extent (commony, Y_AXIS)[dir] 
-       - me->relative_coordinate (commony, Y_AXIS);
+      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;
@@ -347,22 +400,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> columns=
-    Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-columns");
-
-  /*
-    ugh. refps.
-   */
-  Direction d = Directional_element_interface::get (me);
-  *dy = columns.top ()->extent (columns.top (), Y_AXIS) [d]
-    - columns[0]->extent (columns[0], Y_AXIS) [d];
-}
 
 
 /*
@@ -403,16 +440,16 @@ Tuplet_bracket::after_line_breaking (SCM smob)
     }
   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;
@@ -423,7 +460,7 @@ Tuplet_bracket::after_line_breaking (SCM smob)
    */
   Real dy, offset;
   if (!par_beam
-      || Directional_element_interface::get (par_beam) != dir)
+      || get_grob_direction (par_beam) != dir)
     {
       calc_position_and_height (me,&offset,&dy);
     }
@@ -467,22 +504,21 @@ Tuplet_bracket::after_line_breaking (SCM smob)
 
 
 /*
-  similar to slur.
+  similar to beam ? 
  */
 Direction
 Tuplet_bracket::get_default_dir (Grob*me)
 {
-  Direction d = UP;
+  Drul_array<int> dirs(0,0);  
   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) 
-       {
-         d = DOWN;
-         break;
-       }
+      Direction d = Note_column::dir (nc);
+      if (d)
+       dirs[d]++;
     }
-  return d;
+
+  return dirs[UP] >= dirs[DOWN] ? UP : DOWN;
 }
 
 void
@@ -501,5 +537,5 @@ Tuplet_bracket::add_column (Grob*me, Item*n)
 
 ADD_INTERFACE (Tuplet_bracket,"tuplet-bracket-interface",
   "A bracket with a number in the middle, used for tuplets.",
-  "note-columns edge-widen edge-height shorten-pair 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");