]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/grob.cc
Doc-es: various updates.
[lilypond.git] / lily / grob.cc
index 13dd9c7b393c0c7a5e6a009accb5d30d2a1e06da..924c80e500221b2d7bcfc2a8fab0102fea2a7c3c 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--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
@@ -20,6 +20,7 @@
 #include "grob.hh"
 
 #include <cstring>
+#include <set>
 
 #include "align-interface.hh"
 #include "axis-group-interface.hh"
 #include "stencil.hh"
 #include "stream-event.hh"
 #include "system.hh"
+#include "unpure-pure-container.hh"
 #include "warn.hh"
+#include "lily-imports.hh"
 
-#include "ly-smobs.icc"
 
 Grob *
 Grob::clone () const
@@ -49,7 +51,6 @@ Grob::Grob (SCM basicprops)
 {
 
   /* FIXME: default should be no callback.  */
-  self_scm_ = SCM_EOL;
   layout_ = 0;
   original_ = 0;
   interfaces_ = SCM_EOL;
@@ -75,20 +76,33 @@ Grob::Grob (SCM basicprops)
         }
     }
 
-  if (get_property_data ("X-extent") == SCM_EOL)
+  if (scm_is_null (get_property_data ("X-extent")))
     set_property ("X-extent", Grob::stencil_width_proc);
-  if (get_property_data ("Y-extent") == SCM_EOL)
-    set_property ("Y-extent", Grob::stencil_height_proc);
+  if (scm_is_null (get_property_data ("Y-extent")))
+    set_property ("Y-extent",
+                  Unpure_pure_container::make_smob (Grob::stencil_height_proc,
+                                                    Grob::pure_stencil_height_proc));
+  if (scm_is_null (get_property_data ("vertical-skylines")))
+    set_property ("vertical-skylines",
+                  Unpure_pure_container::make_smob (Grob::simple_vertical_skylines_from_extents_proc,
+                                                    Grob::pure_simple_vertical_skylines_from_extents_proc));
+  if (scm_is_null (get_property_data ("horizontal-skylines")))
+    set_property ("horizontal-skylines",
+                  Unpure_pure_container::make_smob (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_)
+  : Smob<Grob> ()
 {
   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;
 
@@ -114,7 +128,7 @@ Grob::get_stencil () const
     return 0;
 
   SCM stil = get_property ("stencil");
-  return unsmob_stencil (stil);
+  return unsmob<Stencil> (stil);
 }
 
 Stencil
@@ -123,20 +137,35 @@ Grob::get_print_stencil () const
   SCM stil = get_property ("stencil");
 
   Stencil retval;
-  if (Stencil *m = unsmob_stencil (stil))
+  if (Stencil *m = unsmob<Stencil> (stil))
     {
       retval = *m;
       bool transparent = to_boolean (get_property ("transparent"));
 
+      /* Process whiteout before color and grob-cause to prevent colored */
+      /* whiteout background and larger file sizes with \pointAndClickOn. */
+      /* A grob has to be visible, otherwise the whiteout property has no effect. */
+      /* Calls the scheme procedure stencil-whiteout in scm/stencils.scm */
+      if (!transparent && (scm_is_number (get_property ("whiteout"))
+                           || to_boolean (get_property ("whiteout"))))
+        {
+          Real line_thickness = layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
+          retval = *unsmob<Stencil>
+            (Lily::stencil_whiteout (retval.smobbed_copy (),
+                                     get_property ("whiteout-style"),
+                                     get_property ("whiteout"),
+                                     scm_from_double (line_thickness)));
+        }
+
       if (transparent)
         retval = Stencil (m->extent_box (), SCM_EOL);
       else
         {
-          SCM expr = m->expr ();
-          expr = scm_list_3 (ly_symbol2scm ("grob-cause"),
-                             self_scm (), expr);
+          SCM expr = scm_list_3 (ly_symbol2scm ("grob-cause"),
+                                 self_scm (),
+                                 retval.expr ());
 
-          retval = Stencil (m->extent_box (), expr);
+          retval = Stencil (retval.extent_box (), expr);
         }
 
       SCM rot = get_property ("rotation");
@@ -160,16 +189,16 @@ Grob::get_print_stencil () const
           retval = Stencil (retval.extent_box (), expr);
         }
 
-      /* process whiteout */
-      /* a grob has to be visible, otherwise the whiteout property has no effect */
-      if (!transparent && to_boolean (get_property ("whiteout")))
+      SCM attributes = get_property ("output-attributes");
+      if (scm_is_pair (attributes))
         {
-          /* Call the scheme procedure stencil-whiteout in scm/stencils.scm */
-          /* to add a round-filled-box stencil to the stencil list */
-          retval
-            = *unsmob_stencil (scm_call_1 (ly_lily_module_constant ("stencil-whiteout"),
-                                           retval.smobbed_copy ()));
+          SCM expr = scm_list_3 (ly_symbol2scm ("output-attributes"),
+                                 attributes,
+                                 retval.expr ());
+
+          retval = Stencil (retval.extent_box (), expr);
         }
+
     }
 
   return retval;
@@ -304,18 +333,21 @@ Real
 Grob::relative_coordinate (Grob const *refp, Axis a) const
 {
   /* eaa - hmmm, should we do a programming_error() here? */
-  if ((this == NULL) || (refp == this))
+  if (refp == this)
     return 0.0;
 
   /* We catch PARENT_L_ == nil case with this, but we crash if we did
      not ask for the absolute coordinate (ie. REFP == nil.)  */
-  Real off = get_offset (a);
-  if (refp == dim_cache_[a].parent_)
-    return off;
 
-  off += dim_cache_[a].parent_->relative_coordinate (refp, a);
+  return get_offset (a) + parent_relative (refp, a);
+}
 
-  return off;
+Real
+Grob::parent_relative (Grob const *refp, Axis a) const
+{
+  if (Grob *p = get_parent (a))
+    return p->relative_coordinate (refp, a);
+  return 0.0;
 }
 
 Real
@@ -356,7 +388,7 @@ Grob::pure_relative_y_coordinate (Grob const *refp, int start, int end)
   if (Grob *p = get_parent (Y_AXIS))
     {
       Real trans = 0;
-      if (Align_interface::has_interface (p) && !dim_cache_[Y_AXIS].offset_)
+      if (has_interface<Align_interface> (p) && !dim_cache_[Y_AXIS].offset_)
         trans = Align_interface::get_pure_child_y_translation (p, this, start, end);
 
       return off + trans + p->pure_relative_y_coordinate (refp, start, end);
@@ -380,7 +412,7 @@ Grob::get_offset (Axis a) const
     UGH: can't fold next 2 statements together. Apparently GCC thinks
     dim_cache_[a].offset_ is unaliased.
   */
-  Real off = robust_scm2double (internal_get_property (sym), 0.0);
+  Real off = robust_scm2double (get_property (sym), 0.0);
   if (me->dim_cache_[a].offset_)
     {
       *me->dim_cache_[a].offset_ += off;
@@ -435,36 +467,36 @@ Grob::extent (Grob *refp, Axis a) const
       /*
         Order is significant: ?-extent may trigger suicide.
        */
-      SCM ext_sym
-        = (a == X_AXIS)
-          ? ly_symbol2scm ("X-extent")
-          : ly_symbol2scm ("Y-extent");
-
-      SCM ext = internal_get_property (ext_sym);
+      SCM ext = (a == X_AXIS)
+              ? get_property ("X-extent")
+              : get_property ("Y-extent");
       if (is_number_pair (ext))
         real_ext.unite (ly_scm2interval (ext));
 
-      SCM min_ext_sym
-        = (a == X_AXIS)
-          ? ly_symbol2scm ("minimum-X-extent")
-          : ly_symbol2scm ("minimum-Y-extent");
-      SCM min_ext = internal_get_property (min_ext_sym);
+      SCM min_ext = (a == X_AXIS)
+              ? get_property ("minimum-X-extent")
+              : get_property ("minimum-Y-extent");
       if (is_number_pair (min_ext))
         real_ext.unite (ly_scm2interval (min_ext));
 
       ((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
+      warning(_f ("ignored infinite %s-offset",
+                        a == X_AXIS ? "X" : "Y"));
 
   return real_ext;
 }
 
 Interval
-Grob::pure_height (Grob *refp, int start, int end)
+Grob::pure_y_extent (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,7 +515,7 @@ Grob::pure_height (Grob *refp, int start, int end)
 Interval
 Grob::maybe_pure_extent (Grob *refp, Axis a, bool pure, int start, int end)
 {
-  return (pure && a == Y_AXIS) ? pure_height (refp, start, end) : extent (refp, a);
+  return (pure && a == Y_AXIS) ? pure_y_extent (refp, start, end) : extent (refp, a);
 }
 
 Interval_t<int>
@@ -598,30 +630,32 @@ Grob::fixup_refpoint ()
   VERTICAL ORDERING
 ****************************************************************/
 
-Grob*
+Grob *
 get_maybe_root_vertical_alignment (Grob *g, Grob *maybe)
 {
   if (!g)
     return maybe;
-  if (Align_interface::has_interface (g))
+  if (has_interface<Align_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 *
 Grob::get_root_vertical_alignment (Grob *g)
 {
   return get_maybe_root_vertical_alignment (g, 0);
 }
 
-Grob*
+Grob *
 Grob::get_vertical_axis_group (Grob *g)
 {
   if (!g)
     return 0;
-  if (Axis_group_interface::has_interface (g)
-      && Align_interface::has_interface (g->get_parent (Y_AXIS)))
+  if (!g->get_parent (Y_AXIS))
+    return 0;
+  if (has_interface<Axis_group_interface> (g)
+      && has_interface<Align_interface> (g->get_parent (Y_AXIS)))
     return g;
   return get_vertical_axis_group (g->get_parent (Y_AXIS));
 
@@ -658,13 +692,12 @@ bool
 Grob::internal_vertical_less (Grob *g1, Grob *g2, bool pure)
 {
   Grob *vag = get_root_vertical_alignment (g1);
-  if (!vag)
-    return false;
   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);
 
@@ -692,32 +725,32 @@ Grob::internal_vertical_less (Grob *g1, Grob *g2, bool pure)
   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))
+  while (Grob *g = unsmob<Grob> (cause))
     cause = g->get_property ("cause");
 
   /* ES TODO: cause can't be Music*/
-  if (Music *m = unsmob_music (cause))
+  if (Music *m = unsmob<Music> (cause))
     m->origin ()->programming_error (s);
-  else if (Stream_event *ev = unsmob_stream_event (cause))
+  else if (Stream_event *ev = unsmob<Stream_event> (cause))
     ev->origin ()->programming_error (s);
   else
     ::programming_error (s);
 }
 
 void
-Grob::warning (string s) const
+Grob::warning (const string &s) const
 {
   SCM cause = self_scm ();
-  while (Grob *g = unsmob_grob (cause))
+  while (Grob *g = unsmob<Grob> (cause))
     cause = g->get_property ("cause");
 
   /* ES TODO: cause can't be Music*/
-  if (Music *m = unsmob_music (cause))
+  if (Music *m = unsmob<Music> (cause))
     m->origin ()->warning (s);
-  else if (Stream_event *ev = unsmob_stream_event (cause))
+  else if (Stream_event *ev = unsmob<Stream_event> (cause))
     ev->origin ()->warning (s);
   else
     ::warning (s);
@@ -729,7 +762,7 @@ Grob::name () const
   SCM meta = get_property ("meta");
   SCM nm = scm_assq (ly_symbol2scm ("name"), meta);
   nm = (scm_is_pair (nm)) ? scm_cdr (nm) : SCM_EOL;
-  return scm_is_symbol (nm) ? ly_symbol2string (nm) : this->class_name ();
+  return scm_is_symbol (nm) ? ly_symbol2string (nm) : class_name ();
 }
 
 ADD_INTERFACE (Grob,
@@ -783,24 +816,28 @@ ADD_INTERFACE (Grob,
                "cause "
                "color "
                "cross-staff "
-               "extra-X-extent "
-               "extra-Y-extent "
                "extra-offset "
+               "footnote-music "
+               "forced-spacing "
+               "horizontal-skylines "
+               "id "
                "interfaces "
                "layer "
                "meta "
                "minimum-X-extent "
                "minimum-Y-extent "
-               "outside-staff-horizontal-padding "
-               "outside-staff-padding "
-               "outside-staff-priority "
+               "output-attributes "
+               "parenthesis-friends "
                "pure-Y-offset-in-progress "
                "rotation "
+               "skyline-horizontal-padding "
                "springs-and-rods "
                "staff-symbol "
                "stencil "
                "transparent "
+               "vertical-skylines "
                "whiteout "
+               "whiteout-style "
               );
 
 /****************************************************************
@@ -821,15 +858,27 @@ MAKE_SCHEME_CALLBACK (Grob, stencil_height, 1);
 SCM
 Grob::stencil_height (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (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)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   Grob *par = me->get_parent (Y_AXIS);
   if (par)
     (void) par->get_property ("positioning-done");
@@ -841,7 +890,7 @@ MAKE_SCHEME_CALLBACK (Grob, x_parent_positioning, 1);
 SCM
 Grob::x_parent_positioning (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
 
   Grob *par = me->get_parent (X_AXIS);
   if (par)
@@ -854,7 +903,7 @@ MAKE_SCHEME_CALLBACK (Grob, stencil_width, 1);
 SCM
 Grob::stencil_width (SCM smob)
 {
-  Grob *me = unsmob_grob (smob);
+  Grob *me = unsmob<Grob> (smob);
   return grob_stencil_extent (me, X_AXIS);
 }
 
@@ -862,7 +911,7 @@ Grob *
 common_refpoint_of_list (SCM elist, Grob *common, Axis a)
 {
   for (; scm_is_pair (elist); elist = scm_cdr (elist))
-    if (Grob *s = unsmob_grob (scm_car (elist)))
+    if (Grob *s = unsmob<Grob> (scm_car (elist)))
       {
         if (common)
           common = common->common_refpoint (s, a);
@@ -885,6 +934,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)
 {
@@ -900,13 +963,60 @@ robust_relative_extent (Grob *me, Grob *refpoint, Axis a)
 bool
 Grob::check_cross_staff (Grob *commony)
 {
-  if (Align_interface::has_interface (commony))
+  if (has_interface<Align_interface> (commony))
     return true;
 
   for (Grob *g = this; g && g != commony; g = g->get_parent (Y_AXIS))
-    if (Align_interface::has_interface (g))
+    if (has_interface<Align_interface> (g))
       return true;
 
   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;
+}