]> git.donarmstrong.com Git - lilypond.git/commitdiff
Issue 5084: Create Bracket class
authorDavid Nalesnik <david.nalesnik@gmail.com>
Thu, 23 Feb 2017 15:57:44 +0000 (09:57 -0600)
committerDavid Nalesnik <david.nalesnik@gmail.com>
Mon, 13 Mar 2017 18:15:50 +0000 (13:15 -0500)
Code involving brackets suffers from two confusing
organizational issues:

(1) Tuplet_bracket::make_bracket is used to create brackets
for a number of grobs: BassFigureBracket, HorizontalBracket,
OttavaBracket, PianoPedalBracket, VoltaBracket, along with
TupletBracket

(2) Methods belonging to Horizontal_bracket are used to draw
both horizonal brackets (HorizontalBracket) and vertical
brackets (BassFigureBracket)

To remedy this, a new Bracket class is created.

This new class contains the old Tuplet_bracket::make_bracket,
Horizontal_bracket::make_bracket, and
Horizontal_bracket::make_enclosing_bracket.

These methods have been renamed to clarify their purpose.

lily/bracket.cc [new file with mode: 0644]
lily/enclosing-bracket.cc
lily/horizontal-bracket.cc
lily/include/bracket.hh [new file with mode: 0644]
lily/include/horizontal-bracket.hh
lily/include/tuplet-bracket.hh
lily/ottava-bracket.cc
lily/piano-pedal-bracket.cc
lily/tuplet-bracket.cc
lily/volta-bracket.cc

diff --git a/lily/bracket.cc b/lily/bracket.cc
new file mode 100644 (file)
index 0000000..8d95b69
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+  This file is part of LilyPond, the GNU music typesetter.
+
+  Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
+  Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+ #include "bracket.hh"
+
+ #include "axis-group-interface.hh"
+ #include "directional-element-interface.hh"
+ #include "grob.hh"
+ #include "lookup.hh"
+ #include "output-def.hh"
+ #include "staff-symbol-referencer.hh"
+ #include "spanner.hh"
+ #include "item.hh"
+ #include "line-interface.hh"
+
+/*
+  should move to lookup?
+
+  TODO: this will fail for very short (shorter than the flare)
+  brackets.
+*/
+Stencil
+Bracket::make_bracket (Grob *me, // for line properties.
+                       Axis protrusion_axis, Offset dz,
+                       Drul_array<Real> height, Interval gap,
+                       Drul_array<Real> flare, Drul_array<Real> shorten)
+{
+  Drul_array<Offset> corners (Offset (0, 0), dz);
+
+  Real length = dz.length ();
+  Drul_array<Offset> gap_corners;
+
+  Axis bracket_axis = other_axis (protrusion_axis);
+
+  Drul_array<Offset> straight_corners = corners;
+
+  for (LEFT_and_RIGHT (d))
+    straight_corners[d] += -d * shorten[d] / length * dz;
+
+  if (!gap.is_empty ())
+    {
+      for (LEFT_and_RIGHT (d))
+        gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
+    }
+
+  Drul_array<Offset> flare_corners = straight_corners;
+  for (LEFT_and_RIGHT (d))
+    {
+      flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
+      flare_corners[d][protrusion_axis] += height[d];
+      straight_corners[d][bracket_axis] += -d * flare[d];
+    }
+
+  Stencil m;
+  if (!gap.is_empty ())
+    for (LEFT_and_RIGHT (d))
+      m.add_stencil (Line_interface::line (me, straight_corners[d],
+                                           gap_corners[d]));
+  else
+    m.add_stencil (Line_interface::line (me, straight_corners[LEFT],
+                                         straight_corners[RIGHT]));
+
+  if (scm_is_number (me->get_property ("dash-fraction")))
+    me->set_property ("dash-fraction", scm_from_double (1.0));
+  for (LEFT_and_RIGHT (d))
+    m.add_stencil (Line_interface::line (me, straight_corners[d],
+                                         flare_corners[d]));
+  return m;
+}
+
+/*
+  Return an ungapped bracket along either the X- or Y-axis.
+*/
+Stencil
+Bracket::make_axis_constrained_bracket (Grob *me, Real length, Axis a,
+                                        Direction dir)
+{
+  Drul_array<Real> edge_height = robust_scm2interval (me->get_property ("edge-height"),
+                                                      Interval (1.0, 1.0));
+  Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
+                                                Interval (0, 0));
+  Drul_array<Real> shorten = robust_scm2interval (me->get_property ("shorten-pair"),
+                                                  Interval (0, 0));
+
+  // Make sure that it points in the correct direction:
+  scale_drul (&edge_height, Real (-dir));
+
+  Offset start;
+  start[a] = length;
+
+  Drul_array<bool> connect_to_other
+    = robust_scm2booldrul (me->get_property ("connect-to-neighbor"),
+                           Drul_array<bool> (false, false));
+
+  for (LEFT_and_RIGHT (d))
+    {
+      if (connect_to_other[d])
+        {
+          edge_height[d] = 0.0;
+          flare[d] = 0.0;
+          shorten[d] = 0.0;
+        }
+    }
+
+  return make_bracket (me, other_axis (a), start, edge_height,
+                       Interval (), flare, shorten);
+}
+
+/*
+  Return an axis-constrained, ungapped bracket which encloses a group of
+  grobs.  Used for analysis brackets (HorizontalBracket) and
+  figured bass (BassFigureBracket).
+*/
+Stencil
+Bracket::make_enclosing_bracket (Grob *me, Grob *refpoint,
+                                 vector<Grob *> grobs, Axis a,
+                                 Direction dir)
+{
+  Grob *common = common_refpoint_of_array (grobs, refpoint, a);
+  Interval ext = Axis_group_interface::relative_group_extent (grobs, common, a);
+
+  if (ext.is_empty ())
+    {
+      me->programming_error ("Can't enclose empty extents with bracket");
+      return Stencil ();
+    }
+  else
+    {
+      Stencil b = make_axis_constrained_bracket (me, ext.length (), a, dir);
+      b.translate_axis (ext[LEFT] - refpoint->relative_coordinate (common, a), a);
+
+      return b;
+    }
+}
index dd551e38414078c28ad515f4c57fec667af3da61..3ccc62b6682a72c38d8e3dae5b8d729559f99379 100644 (file)
@@ -19,7 +19,7 @@
 */
 
 #include "stencil.hh"
-#include "horizontal-bracket.hh"
+#include "bracket.hh"
 #include "grob.hh"
 #include "axis-group-interface.hh"
 #include "pointer-group-interface.hh"
@@ -64,8 +64,10 @@ Enclosing_bracket::width (SCM grob)
   Grob *common_x = common_refpoint_of_array (elements, me, X_AXIS);
   Interval xext = Axis_group_interface::relative_group_extent (elements, common_x, X_AXIS);
 
-  Stencil left_br = Horizontal_bracket::make_bracket (me, 10.0, Y_AXIS, LEFT);
-  Stencil right_br = Horizontal_bracket::make_bracket (me, 10.0, Y_AXIS, LEFT);
+  Stencil left_br =
+    Bracket::make_axis_constrained_bracket (me, 10.0, Y_AXIS, LEFT);
+  Stencil right_br =
+    Bracket::make_axis_constrained_bracket (me, 10.0, Y_AXIS, LEFT);
 
   xext.widen (robust_scm2double (me->get_property ("padding"), 0.25));
   left_br.translate_axis (xext[LEFT], X_AXIS);
@@ -97,10 +99,10 @@ Enclosing_bracket::print (SCM grob)
       xext = Interval (0, 0);
     }
 
-  Stencil left_br = Horizontal_bracket::make_enclosing_bracket (me, me, elements,
-                    Y_AXIS, LEFT);
-  Stencil right_br = Horizontal_bracket::make_enclosing_bracket (me, me, elements,
-                     Y_AXIS, RIGHT);
+  Stencil left_br =
+    Bracket::make_enclosing_bracket (me, me, elements, Y_AXIS, LEFT);
+  Stencil right_br =
+    Bracket::make_enclosing_bracket (me, me, elements, Y_AXIS, RIGHT);
 
   xext.widen (robust_scm2double (me->get_property ("padding"), 0.25));
   left_br.translate_axis (xext[LEFT], X_AXIS);
index cf37516d5c0c0b697659f10d003ffafd05ec9392..b453985b091b6ef31c7ff16f3b6f5110597d6232 100644 (file)
 
 #include "horizontal-bracket.hh"
 
-#include "lookup.hh"
-#include "side-position-interface.hh"
+#include "bracket.hh"
+#include "stencil.hh"
 #include "pointer-group-interface.hh"
 #include "directional-element-interface.hh"
-#include "output-def.hh"
-#include "staff-symbol-referencer.hh"
-#include "tuplet-bracket.hh"
-#include "axis-group-interface.hh"
 #include "spanner.hh"
 #include "item.hh"
 
-Stencil
-Horizontal_bracket::make_bracket (Grob *me,
-                                  Real length,
-                                  Axis a, Direction dir)
-{
-  Drul_array<Real> edge_height = robust_scm2interval (me->get_property ("edge-height"),
-                                                      Interval (1.0, 1.0));
-  Drul_array<Real> flare = robust_scm2interval (me->get_property ("bracket-flare"),
-                                                Interval (0, 0));
-  Drul_array<Real> shorten = robust_scm2interval (me->get_property ("shorten-pair"),
-                                                  Interval (0, 0));
-
-  // Make sure that it points in the correct direction:
-  scale_drul (&edge_height, Real (-dir));
-
-  Interval empty;
-  Offset start;
-  start[a] = length;
-
-  Drul_array<bool> connect_to_other
-    = robust_scm2booldrul (me->get_property ("connect-to-neighbor"),
-                           Drul_array<bool> (false, false));
-
-  for (LEFT_and_RIGHT (d))
-    {
-      if (connect_to_other[d])
-        {
-          edge_height[d] = 0.0;
-          flare[d] = 0.0;
-          shorten[d] = 0.0;
-        }
-    }
-
-  /*
-    ugh, Tuplet_bracket should use Horizontal_bracket, not the other way around.
-  */
-  return Tuplet_bracket::make_bracket (me, other_axis (a), start,
-                                       edge_height, empty, flare, shorten);
-}
-
-Stencil
-Horizontal_bracket::make_enclosing_bracket (Grob *me, Grob *refpoint,
-                                            vector<Grob *> grobs,
-                                            Axis a, Direction dir)
-{
-  Grob *common = common_refpoint_of_array (grobs, refpoint, a);
-  Interval ext = Axis_group_interface::relative_group_extent (grobs, common, a);
-
-  if (ext.is_empty ())
-    {
-      me->programming_error ("Can't enclose empty extents with bracket");
-      return Stencil ();
-    }
-  else
-    {
-      Stencil b = make_bracket (me, ext.length (), a, dir);
-      b.translate_axis (ext[LEFT] - refpoint->relative_coordinate (common, a), a);
-
-      return b;
-    }
-}
-
 MAKE_SCHEME_CALLBACK (Horizontal_bracket, print, 1);
 SCM
 Horizontal_bracket::print (SCM smob)
@@ -113,7 +47,8 @@ Horizontal_bracket::print (SCM smob)
         enclosed.push_back (b);
     }
 
-  Stencil b = make_enclosing_bracket (me, me, enclosed, X_AXIS, get_grob_direction (me));
+  Stencil b = Bracket::make_enclosing_bracket (me, me, enclosed, X_AXIS,
+                                               get_grob_direction (me));
   return b.smobbed_copy ();
 }
 
diff --git a/lily/include/bracket.hh b/lily/include/bracket.hh
new file mode 100644 (file)
index 0000000..c58ebfc
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+  This file is part of LilyPond, the GNU music typesetter.
+
+  Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef BRACKET_HH
+#define BRACKET_HH
+
+#include "lily-proto.hh"
+#include "grob-interface.hh"
+#include "std-vector.hh"
+
+struct Bracket
+{
+  static Stencil make_bracket (Grob *me, Axis protrusion_axis,
+                               Offset dz, Drul_array<Real> height,
+                               Interval gap, Drul_array<Real> flare,
+                               Drul_array<Real> shorten);
+  static Stencil make_axis_constrained_bracket (Grob *me, Real length,
+                                                Axis a, Direction dir);
+  static Stencil make_enclosing_bracket (Grob *me, Grob *refpoint,
+                                         vector<Grob *> grobs,
+                                         Axis a, Direction dir);
+};
+
+#endif /* BRACKET_HH */
index 3a1692fc1124b216e04c253f064263cfa27bf0e9..bfc76b7a0d4c9a33139ca7ba4e2f0dc1e49a9639 100644 (file)
 #define HORIZONTAL_BRACKET_HH
 
 #include "lily-proto.hh"
-#include "std-vector.hh"
 #include "grob-interface.hh"
 
 struct Horizontal_bracket
 {
   DECLARE_SCHEME_CALLBACK (print, (SCM));
-  static Stencil make_bracket (Grob *, Real, Axis, Direction);
-  static Stencil make_enclosing_bracket (Grob *me, Grob *refpoint,
-                                         vector<Grob *> grobs,
-                                         Axis a, Direction dir);
 };
 
 #endif /* HORIZONTAL_BRACKET_HH */
index 1e41154fbf32dfd03755ffd524cf1a6de81a8230..cfe1ca1481414257be6d7a3a8dca69a7b44e4d86 100644 (file)
@@ -43,10 +43,6 @@ public:
   static Grob *parallel_beam (Grob *me, vector<Grob *> const &cols,
                               bool *equally_long);
   static void calc_position_and_height (Grob *, Real *, Real *dy);
-  static Stencil make_bracket (Grob *me, Axis protrusion_axis,
-                               Offset dz, Drul_array<Real> height,
-                               Interval gap, Drul_array<Real> widen,
-                               Drul_array<Real> shorten);
   static Direction get_default_dir (Grob *);
 };
 
index 63a9d3ebca37adaa2b60dd4bce024875b2301202..cc33d8b152fd4a311a769d980184d2800bb1b37f 100644 (file)
@@ -28,7 +28,7 @@
 #include "staff-symbol-referencer.hh"
 #include "note-column.hh"
 #include "directional-element-interface.hh"
-#include "tuplet-bracket.hh"
+#include "bracket.hh"
 #include "rhythmic-head.hh"
 #include "pointer-group-interface.hh"
 
@@ -151,12 +151,11 @@ Ottava_bracket::print (SCM smob)
 
   Stencil b;
   Interval empty;
+
   if (!bracket_span_points.is_empty () && bracket_span_points.length () > 0.001)
-    b = Tuplet_bracket::make_bracket (me,
-                                      Y_AXIS, Offset (bracket_span_points.length (), 0),
-                                      edge_height,
-                                      empty,
-                                      flare, Drul_array<Real> (0, 0));
+    b = Bracket::make_bracket (
+      me, Y_AXIS, Offset (bracket_span_points.length (), 0),
+      edge_height, empty, flare, Drul_array<Real> (0, 0));
 
   /*
    * The vertical lines should not take space, for the following scenario:
index b47e5e3b6deee146c2f20a51d49c05353064273d..d13f56414f4f52c3f8939f6b7c306a94360477d9 100644 (file)
@@ -20,7 +20,7 @@
 #include "stencil.hh"
 #include "spanner.hh"
 #include "item.hh"
-#include "tuplet-bracket.hh"
+#include "bracket.hh"
 #include "axis-group-interface.hh"
 
 struct Piano_pedal_bracket
@@ -89,11 +89,9 @@ Piano_pedal_bracket::print (SCM smob)
   if (!span_points.is_empty ()
       && span_points.length () > 0.001)
     {
-      m = Tuplet_bracket::make_bracket (me, Y_AXIS,
-                                        Offset (span_points.length (), 0),
-                                        height,
-                                        Interval (),
-                                        flare, shorten);
+      m = Bracket::make_bracket (
+        me, Y_AXIS, Offset (span_points.length (), 0), height,
+        Interval (), flare, shorten);
     }
   m.translate_axis (span_points[LEFT]
                     - me->relative_coordinate (common, X_AXIS), X_AXIS);
index 340b017723753e5431ecce6379129f65ad212caf..3228c9f7b6edab7b79b7c7dbea8500b3284fafe0 100644 (file)
@@ -41,6 +41,8 @@
 */
 
 #include "tuplet-bracket.hh"
+
+#include "bracket.hh"
 #include "axis-group-interface.hh"
 #include "line-interface.hh"
 #include "beam.hh"
@@ -373,15 +375,15 @@ Tuplet_bracket::print (SCM smob)
             }
         }
 
-      Stencil brack = make_bracket (me, Y_AXIS,
-                                    points[RIGHT] - points[LEFT],
-                                    height,
-                                    /*
-                                      0.1 = more space at right due to italics
-                                      TODO: use italic correction of font.
-                                    */
-                                    Interval (-0.5, 0.5) * gap + 0.1,
-                                    flare, shorten);
+      Stencil brack =
+        Bracket::make_bracket (
+          me, Y_AXIS, points[RIGHT] - points[LEFT], height,
+          /*
+            0.1 = more space at right due to italics
+            TODO: use italic correction of font.
+          */
+          Interval (-0.5, 0.5) * gap + 0.1,
+          flare, shorten);
 
       for (LEFT_and_RIGHT (d))
         {
@@ -396,64 +398,6 @@ Tuplet_bracket::print (SCM smob)
   return mol.smobbed_copy ();
 }
 
-/*
-  should move to lookup?
-
-  TODO: this will fail for very short (shorter than the flare)
-  brackets.
-*/
-Stencil
-Tuplet_bracket::make_bracket (Grob *me, // for line properties.
-                              Axis protrusion_axis,
-                              Offset dz,
-                              Drul_array<Real> height,
-                              Interval gap,
-                              Drul_array<Real> flare,
-                              Drul_array<Real> shorten)
-{
-  Drul_array<Offset> corners (Offset (0, 0), dz);
-
-  Real length = dz.length ();
-  Drul_array<Offset> gap_corners;
-
-  Axis bracket_axis = other_axis (protrusion_axis);
-
-  Drul_array<Offset> straight_corners = corners;
-
-  for (LEFT_and_RIGHT (d))
-    straight_corners[d] += -d * shorten[d] / length * dz;
-
-  if (!gap.is_empty ())
-    {
-      for (LEFT_and_RIGHT (d))
-        gap_corners[d] = (dz * 0.5) + gap[d] / length * dz;
-    }
-
-  Drul_array<Offset> flare_corners = straight_corners;
-  for (LEFT_and_RIGHT (d))
-    {
-      flare_corners[d][bracket_axis] = straight_corners[d][bracket_axis];
-      flare_corners[d][protrusion_axis] += height[d];
-      straight_corners[d][bracket_axis] += -d * flare[d];
-    }
-
-  Stencil m;
-  if (!gap.is_empty ())
-    for (LEFT_and_RIGHT (d))
-      m.add_stencil (Line_interface::line (me, straight_corners[d],
-                                           gap_corners[d]));
-  else
-    m.add_stencil (Line_interface::line (me, straight_corners[LEFT],
-                                         straight_corners[RIGHT]));
-
-  if (scm_is_number (me->get_property ("dash-fraction")))
-    me->set_property ("dash-fraction", scm_from_double (1.0));
-  for (LEFT_and_RIGHT (d))
-    m.add_stencil (Line_interface::line (me, straight_corners[d],
-                                         flare_corners[d]));
-  return m;
-}
-
 void
 Tuplet_bracket::get_bounds (Grob *me, Grob **left, Grob **right)
 {
index 9ac2b44637f5fab3d24cd0d6235ea384d6faada9..e337a34b34320c6f4a1b18b95edb205688ac399a 100644 (file)
@@ -31,7 +31,7 @@ using namespace std;
 #include "side-position-interface.hh"
 #include "directional-element-interface.hh"
 #include "lookup.hh"
-#include "tuplet-bracket.hh"
+#include "bracket.hh"
 #include "lily-imports.hh"
 
 /*
@@ -94,12 +94,9 @@ Volta_bracket_interface::print (SCM smob)
   Offset start;
   start[X_AXIS] = me->spanner_length () - left;
 
-  /*
-    ugh, Tuplet_bracket should use Horizontal_bracket, not the other way around.
-  */
   Stencil total
-    = Tuplet_bracket::make_bracket (me, Y_AXIS, start,
-                                    edge_height, empty, flare, shorten);
+    = Bracket::make_bracket (me, Y_AXIS, start, edge_height, empty,
+                             flare, shorten);
 
   if (!orig_span || broken_first_bracket)
     {