]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/grob.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / grob.cc
index d8aeedd5ad2d3f53fff33bfd5761e5ff7e91953a..86ec7e4c6be40b7bc16cf7c3a58b6b631d705049 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 1997--2014 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
@@ -20,6 +20,7 @@
 #include "grob.hh"
 
 #include <cstring>
+#include <set>
 
 #include "align-interface.hh"
 #include "axis-group-interface.hh"
@@ -35,6 +36,7 @@
 #include "stencil.hh"
 #include "stream-event.hh"
 #include "system.hh"
+#include "unpure-pure-container.hh"
 #include "warn.hh"
 
 #include "ly-smobs.icc"
@@ -78,17 +80,30 @@ Grob::Grob (SCM basicprops)
   if (get_property_data ("X-extent") == SCM_EOL)
     set_property ("X-extent", Grob::stencil_width_proc);
   if (get_property_data ("Y-extent") == SCM_EOL)
-    set_property ("Y-extent", Grob::stencil_height_proc);
+    set_property ("Y-extent",
+                  ly_make_unpure_pure_container (Grob::stencil_height_proc,
+                                                 Grob::pure_stencil_height_proc));
+  if (get_property_data ("vertical-skylines") == SCM_EOL)
+    set_property ("vertical-skylines",
+                  ly_make_unpure_pure_container (Grob::simple_vertical_skylines_from_extents_proc,
+                                                 Grob::pure_simple_vertical_skylines_from_extents_proc));
+  if (get_property_data ("horizontal-skylines") == SCM_EOL)
+    set_property ("horizontal-skylines",
+                  ly_make_unpure_pure_container (Grob::simple_horizontal_skylines_from_extents_proc,
+                                                 Grob::pure_simple_horizontal_skylines_from_extents_proc));
 }
 
 Grob::Grob (Grob const &s)
-  : dim_cache_ (s.dim_cache_)
 {
   original_ = (Grob *) & s;
   self_scm_ = SCM_EOL;
 
   immutable_property_alist_ = s.immutable_property_alist_;
   mutable_property_alist_ = SCM_EOL;
+
+  for (Axis a = X_AXIS; a < NO_AXES; incr (a))
+      dim_cache_ [a] = s.dim_cache_ [a];
+
   interfaces_ = s.interfaces_;
   object_alist_ = SCM_EOL;
 
@@ -170,6 +185,17 @@ Grob::get_print_stencil () const
             = *unsmob_stencil (scm_call_1 (ly_lily_module_constant ("stencil-whiteout"),
                                            retval.smobbed_copy ()));
         }
+
+      SCM id = get_property ("id");
+      if (scm_is_string (id))
+        {
+          SCM expr = scm_list_3 (ly_symbol2scm ("id"),
+                                 id,
+                                 retval.expr ());
+
+          retval = Stencil (retval.extent_box (), expr);
+        }
+
     }
 
   return retval;
@@ -286,7 +312,7 @@ Grob::translate_axis (Real y, Axis a)
 {
   if (isinf (y) || isnan (y))
     {
-      programming_error (_ ("Infinity or NaN encountered"));
+      programming_error ("Infinity or NaN encountered");
       return;
     }
 
@@ -455,7 +481,12 @@ Grob::extent (Grob *refp, Axis a) const
       ((Grob *)this)->dim_cache_[a].extent_ = new Interval (real_ext);
     }
 
-  real_ext.translate (offset);
+  // We never want nan, so we avoid shifting infinite values.
+    if(!isinf (offset))
+      real_ext.translate(offset);
+    else
+      this->warning(_f ("ignored infinite %s-offset",
+                        a == X_AXIS ? "X" : "Y"));
 
   return real_ext;
 }
@@ -464,7 +495,7 @@ Interval
 Grob::pure_height (Grob *refp, int start, int end)
 {
   SCM iv_scm = get_pure_property ("Y-extent", start, end);
-  Interval iv = robust_scm2interval (iv_scm, Interval (0, 0));
+  Interval iv = robust_scm2interval (iv_scm, Interval ());
   Real offset = pure_relative_y_coordinate (refp, start, end);
 
   SCM min_ext = get_property ("minimum-Y-extent");
@@ -483,8 +514,6 @@ Grob::pure_height (Grob *refp, int start, int end)
 Interval
 Grob::maybe_pure_extent (Grob *refp, Axis a, bool pure, int start, int end)
 {
-  if (pure && a != Y_AXIS)
-    programming_error ("tried to get pure width");
   return (pure && a == Y_AXIS) ? pure_height (refp, start, end) : extent (refp, a);
 }
 
@@ -515,15 +544,39 @@ Grob::less (Grob *g1, Grob *g2)
 Grob *
 Grob::common_refpoint (Grob const *s, Axis a) const
 {
-  /* I don't like the quadratic aspect of this code, but I see no
-     other way.  The largest chain of parents might be 10 high or so,
-     so it shouldn't be a real issue.  */
-  for (Grob const *c = this; c; c = c->dim_cache_[a].parent_)
-    for (Grob const *d = s; d; d = d->dim_cache_[a].parent_)
-      if (d == c)
-        return (Grob *) d;
 
-  return 0;
+  /* Catching the trivial cases is likely costlier than just running
+     through: one can't avoid going to the respective chain ends
+     anyway.  We might save the second run through when the chain ends
+     differ, but keeping track of the ends makes the loop more costly.
+  */
+
+  int balance = 0;
+  Grob const *c;
+  Grob const *d;
+
+  for (c = this; c; ++balance)
+    c = c->dim_cache_[a].parent_;
+
+  for (d = s; d; --balance)
+    d = d->dim_cache_[a].parent_;
+
+  /* Cut down ancestry to same size */
+
+  for (c = this; balance > 0; --balance)
+    c = c->dim_cache_[a].parent_;
+
+  for (d = s; balance < 0; ++balance)
+    d = d->dim_cache_[a].parent_;
+
+  /* Now find point where our lineages converge */
+  while (c != d)
+    {
+      c = c->dim_cache_[a].parent_;
+      d = d->dim_cache_[a].parent_;
+    }
+
+  return (Grob *) c;
 }
 
 void
@@ -572,11 +625,106 @@ Grob::fixup_refpoint ()
     }
 }
 
+/****************************************************************
+  VERTICAL ORDERING
+****************************************************************/
+
+Grob *
+get_maybe_root_vertical_alignment (Grob *g, Grob *maybe)
+{
+  if (!g)
+    return maybe;
+  if (Align_interface::has_interface (g))
+    return get_maybe_root_vertical_alignment (g->get_parent (Y_AXIS), g);
+  return get_maybe_root_vertical_alignment (g->get_parent (Y_AXIS), maybe);
+
+}
+
+Grob *
+Grob::get_root_vertical_alignment (Grob *g)
+{
+  return get_maybe_root_vertical_alignment (g, 0);
+}
+
+Grob *
+Grob::get_vertical_axis_group (Grob *g)
+{
+  if (!g)
+    return 0;
+  if (!g->get_parent (Y_AXIS))
+    return 0;
+  if (Axis_group_interface::has_interface (g)
+      && Align_interface::has_interface (g->get_parent (Y_AXIS)))
+    return g;
+  return get_vertical_axis_group (g->get_parent (Y_AXIS));
+
+}
+
+int
+Grob::get_vertical_axis_group_index (Grob *g)
+{
+  Grob *val = get_root_vertical_alignment (g);
+  if (!val)
+    return -1;
+  Grob *vax = get_vertical_axis_group (g);
+  extract_grob_set (val, "elements", elts);
+  for (vsize i = 0; i < elts.size (); i++)
+    if (elts[i] == vax)
+      return (int) i;
+  g->programming_error ("could not find this grob's vertical axis group in the vertical alignment");
+  return -1;
+}
+
+bool
+Grob::vertical_less (Grob *g1, Grob *g2)
+{
+  return internal_vertical_less (g1, g2, false);
+}
+
+bool
+Grob::pure_vertical_less (Grob *g1, Grob *g2)
+{
+  return internal_vertical_less (g1, g2, true);
+}
+
+bool
+Grob::internal_vertical_less (Grob *g1, Grob *g2, bool pure)
+{
+  Grob *vag = get_root_vertical_alignment (g1);
+  if (!vag)
+    {
+      g1->programming_error ("grob does not belong to a VerticalAlignment?");
+      return false;
+    }
+
+  Grob *ag1 = get_vertical_axis_group (g1);
+  Grob *ag2 = get_vertical_axis_group (g2);
+
+  extract_grob_set (vag, "elements", elts);
+
+  if (ag1 == ag2 && !pure)
+    {
+      Grob *common = g1->common_refpoint (g2, Y_AXIS);
+      return g1->relative_coordinate (common, Y_AXIS) > g2->relative_coordinate (common, Y_AXIS);
+    }
+
+  for (vsize i = 0; i < elts.size (); i++)
+    {
+      if (elts[i] == ag1)
+        return true;
+      if (elts[i] == ag2)
+        return false;
+    }
+
+  g1->programming_error ("could not place this grob in its axis group");
+  return false;
+}
+
 /****************************************************************
   MESSAGES
 ****************************************************************/
 void
-Grob::programming_error (string s) const
+Grob::programming_error (const string &s) const
 {
   SCM cause = self_scm ();
   while (Grob *g = unsmob_grob (cause))
@@ -592,7 +740,7 @@ Grob::programming_error (string s) const
 }
 
 void
-Grob::warning (string s) const
+Grob::warning (const string &s) const
 {
   SCM cause = self_scm ();
   while (Grob *g = unsmob_grob (cause))
@@ -667,23 +815,24 @@ ADD_INTERFACE (Grob,
                "cause "
                "color "
                "cross-staff "
-               "extra-X-extent "
-               "extra-Y-extent "
+               "id "
                "extra-offset "
+               "footnote-music "
+               "forced-spacing "
+               "horizontal-skylines "
                "interfaces "
                "layer "
                "meta "
                "minimum-X-extent "
                "minimum-Y-extent "
-               "outside-staff-horizontal-padding "
-               "outside-staff-padding "
-               "outside-staff-priority "
                "pure-Y-offset-in-progress "
                "rotation "
+               "skyline-horizontal-padding "
                "springs-and-rods "
                "staff-symbol "
                "stencil "
                "transparent "
+               "vertical-skylines "
                "whiteout "
               );
 
@@ -709,6 +858,18 @@ Grob::stencil_height (SCM smob)
   return grob_stencil_extent (me, Y_AXIS);
 }
 
+MAKE_SCHEME_CALLBACK (Grob, pure_stencil_height, 3);
+SCM
+Grob::pure_stencil_height (SCM smob, SCM /* beg */, SCM /* end */)
+{
+  Grob *me = unsmob_grob (smob);
+  if (unsmob_stencil (me->get_property_data ("stencil")))
+    return grob_stencil_extent (me, Y_AXIS);
+
+  return ly_interval2scm (Interval ());
+
+}
+
 MAKE_SCHEME_CALLBACK (Grob, y_parent_positioning, 1);
 SCM
 Grob::y_parent_positioning (SCM smob)
@@ -769,6 +930,20 @@ common_refpoint_of_array (vector<Grob *> const &arr, Grob *common, Axis a)
   return common;
 }
 
+Grob *
+common_refpoint_of_array (set<Grob *> const &arr, Grob *common, Axis a)
+{
+  set<Grob *>::iterator it;
+
+  for (it = arr.begin (); it != arr.end (); it++)
+    if (common)
+      common = common->common_refpoint (*it, a);
+    else
+      common = *it;
+
+  return common;
+}
+
 Interval
 robust_relative_extent (Grob *me, Grob *refpoint, Axis a)
 {
@@ -794,3 +969,50 @@ Grob::check_cross_staff (Grob *commony)
   return false;
 }
 
+static
+bool
+indirect_less (Grob **a, Grob **b)
+{
+  // Use original order as tie breaker.  That gives us a stable sort
+  // at the lower price tag of an unstable one, and we want a stable
+  // sort in order to reliably retain the first instance of a grob
+  // pointer.
+  return *a < *b || (*a == *b && a < b);
+}
+
+static
+bool
+indirect_eq (Grob **a, Grob **b)
+{
+  return *a == *b;
+}
+
+static
+bool
+direct_less (Grob **a, Grob **b)
+{
+  return a < b;
+}
+
+// uniquify uniquifies on the memory addresses of the Grobs, but then
+// uses the original order.  This makes results independent from the
+// memory allocation of Grobs.
+
+void
+uniquify (vector <Grob *> & grobs)
+{
+  vector <Grob **> vec (grobs.size ());
+  for (vsize i = 0; i < grobs.size (); i++)
+    vec[i] = &grobs[i];
+  vector_sort (vec, indirect_less);
+  vec.erase (unique (vec.begin (), vec.end (), indirect_eq), vec.end ());
+  vector_sort (vec, direct_less);
+
+  // Since the output is a sorted copy of the input with some elements
+  // removed, we can fill in the vector in-place if we do it starting
+  // from the front.
+  for (vsize i = 0; i < vec.size (); i++)
+    grobs[i] = *vec[i];
+  grobs.erase (grobs.begin () + vec.size (), grobs.end ());
+  return;
+}