]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/grob.cc
* lily/grob.cc (get_stencil): simplify: use callback mechanism to
[lilypond.git] / lily / grob.cc
index 87e81ebef90525dc01defa6052551623bc008ca4..b85e61824d12a5ece5211269e2e000a6e54e8f4e 100644 (file)
@@ -9,12 +9,11 @@
 #include "grob.hh"
 
 #include <cstring>
-#include <math.h>
 
 #include "main.hh"
 #include "input-smob.hh"
 #include "warn.hh"
-#include "group-interface.hh"
+#include "pointer-group-interface.hh"
 #include "misc.hh"
 #include "paper-score.hh"
 #include "stencil.hh"
 #include "ly-smobs.icc"
 #include "output-def.hh"
 
+MAKE_SCHEME_CALLBACK(Grob, same_axis_parent_positioning, 2);
+SCM
+Grob::same_axis_parent_positioning (SCM element_smob, SCM axis)
+{
+  Grob *me = unsmob_grob (element_smob);
+  Axis ax = Axis (scm_to_int (axis));
+  
+  Grob *par = me->get_parent (ax);
+  if (par)
+    par->get_property ("positioning-done");
+
+  return scm_from_double (0.0);
+}
+
+MAKE_SCHEME_CALLBACK(Grob,other_axis_parent_positioning, 2);
+SCM
+Grob::other_axis_parent_positioning (SCM element_smob, SCM axis)
+{
+  Grob *me = unsmob_grob (element_smob);
+  Axis ax = other_axis ((Axis) scm_to_int (axis));
+  
+  Grob *par = me->get_parent (ax);
+  if (par)
+    par->get_property ("positioning-done");
+
+  return scm_from_double (0.0);
+}
+
+
+
 Grob *
 Grob::clone (int count) const
 {
@@ -52,9 +81,12 @@ Grob::Grob (SCM basicprops,
   pscore_ = 0;
   status_ = 0;
   original_ = 0;
+  interfaces_ = SCM_EOL;
   immutable_property_alist_ = basicprops;
   mutable_property_alist_ = SCM_EOL;
-
+  object_alist_ = SCM_EOL;
+  property_callbacks_ = SCM_EOL;
+  
   /* We do smobify_self () as the first step.  Since the object lives
      on the heap, none of its SCM variables are protected from
      GC. After smobify_self (), they are.  */
@@ -63,18 +95,12 @@ Grob::Grob (SCM basicprops,
   /*
     We always get a new key object for a new grob.
   */
-  scm_gc_unprotect_object (key_->self_scm ());
+  if (key_)
+    ((Object_key *)key_)->unprotect ();
+
   SCM meta = get_property ("meta");
   if (scm_is_pair (meta))
-    {
-      SCM ifs = scm_assoc (ly_symbol2scm ("interfaces"), meta);
-
-      /* Switch off interface checks for the moment.  */
-      bool itc = do_internal_type_checking_global;
-      do_internal_type_checking_global = false;
-      internal_set_property (ly_symbol2scm ("interfaces"), scm_cdr (ifs));
-      do_internal_type_checking_global = itc;
-    }
+    interfaces_ = scm_cdr (scm_assoc (ly_symbol2scm ("interfaces"), meta));
 
   /* TODO:
 
@@ -83,13 +109,24 @@ Grob::Grob (SCM basicprops,
   creation. Convenient eg. when using \override with
   StaffSymbol.  */
 
-  char const *onames[] = {"X-offset-callbacks", "Y-offset-callbacks"};
-  char const *xnames[] = {"X-extent", "Y-extent"};
-  char const *enames[] = {"X-extent-callback", "Y-extent-callback"};
+  property_callbacks_ = get_property ("callbacks");
+
+  SCM off_callbacks[] = {
+    get_property ("X-offset-callbacks"),
+    get_property ("Y-offset-callbacks")
+  };
+  SCM extents[] = {
+    get_property ("X-extent"),
+    get_property ("Y-extent")
+  };
+  SCM extent_callbacks[] = {
+    get_property ("X-extent-callback"),
+    get_property ("Y-extent-callback")
+  };
 
   for (int a = X_AXIS; a <= Y_AXIS; a++)
     {
-      SCM l = get_property (onames[a]);
+      SCM l = off_callbacks[a];
 
       if (scm_ilength (l) >= 0)
        {
@@ -99,44 +136,41 @@ Grob::Grob (SCM basicprops,
       else
        programming_error ("[XY]-offset-callbacks must be a list");
 
-      SCM cb = get_property (enames[a]);
+      SCM cb = extent_callbacks[a];
       if (cb == SCM_BOOL_F)
-       {
-         dim_cache_[a].dimension_ = SCM_BOOL_F;
-       }
+       dim_cache_[a].dimension_ = SCM_BOOL_F;
 
-      SCM xt = get_property (xnames[a]);
+      SCM xt = extents[a];
       if (is_number_pair (xt))
-       {
-         dim_cache_[a].dimension_ = xt;
-       }
+       dim_cache_[a].dimension_ = xt;
       else if (ly_is_procedure (cb))
-       {
-         dim_cache_[a].dimension_callback_ = cb;
-       }
+       dim_cache_[a].dimension_callback_ = cb;
       else if (cb == SCM_EOL
-              && ly_is_procedure (get_property ("print-function")))
+              && ly_is_procedure (ly_assoc_get (ly_symbol2scm ("stencil"),
+                                                property_callbacks_, SCM_BOOL_F)))
        dim_cache_[a].dimension_callback_ = stencil_extent_proc;
     }
+
 }
 
 Grob::Grob (Grob const &s, int copy_index)
   : dim_cache_ (s.dim_cache_)
 {
-  key_ = new Copied_key (s.key_, copy_index);
+  key_ = (use_object_keys) ? new Copied_key (s.key_, copy_index) : 0;
   original_ = (Grob *) & s;
   self_scm_ = SCM_EOL;
 
   immutable_property_alist_ = s.immutable_property_alist_;
-  mutable_property_alist_ = SCM_EOL;
+  mutable_property_alist_ = ly_deep_copy (s.mutable_property_alist_);
+  interfaces_ = s.interfaces_;
+  property_callbacks_ = s.property_callbacks_;
+  object_alist_ = SCM_EOL;
 
-  /* No properties are copied.  That is the job of
-     handle_broken_dependencies.  */
-  status_ = s.status_;
   pscore_ = 0;
 
   smobify_self ();
-  scm_gc_unprotect_object (key_->self_scm ());
+  if (key_)
+    ((Object_key *)key_)->unprotect ();
 }
 
 Grob::~Grob ()
@@ -162,9 +196,7 @@ robust_relative_extent (Grob *me, Grob *refp, Axis a)
 {
   Interval ext = me->extent (refp, a);
   if (ext.is_empty ())
-    {
-      ext.add_point (me->relative_coordinate (refp, a));
-    }
+    ext.add_point (me->relative_coordinate (refp, a));
 
   return ext;
 }
@@ -175,36 +207,6 @@ Grob::get_layout () const
   return pscore_ ? pscore_->layout () : 0;
 }
 
-/* Recursively track all dependencies of this Grob.  The status_ field
-   is used as a mark-field.  It is marked with BUSY during execution
-   of this function, and marked with FINAL when finished.
-
-   FUNCPTR is the function to call to update this element.  */
-void
-Grob::calculate_dependencies (int final, int busy, SCM funcname)
-{
-  if (status_ >= final)
-    return;
-
-  if (status_ == busy)
-    {
-      programming_error ("element is busy, come back later");
-      return;
-    }
-
-  status_ = busy;
-
-  for (SCM d = get_property ("dependencies"); scm_is_pair (d);
-       d = scm_cdr (d))
-    unsmob_grob (scm_car (d))->calculate_dependencies (final, busy, funcname);
-
-  SCM proc = internal_get_property (funcname);
-  if (ly_is_procedure (proc))
-    scm_call_1 (proc, this->self_scm ());
-
-  status_ = final;
-}
-
 Stencil *
 Grob::get_stencil () const
 {
@@ -212,39 +214,27 @@ Grob::get_stencil () const
     return 0;
 
   SCM stil = get_property ("stencil");
-  if (unsmob_stencil (stil))
-    return unsmob_stencil (stil);
-
-  stil = get_uncached_stencil ();
-  if (is_live ())
-    {
-      Grob *me = (Grob *) this;
-      me->set_property ("stencil", stil);
-    }
-
   return unsmob_stencil (stil);
 }
 
-SCM
-Grob::get_uncached_stencil () const
+Stencil
+Grob::get_print_stencil () const
 {
-  SCM proc = get_property ("print-function");
-
-  SCM stil = SCM_EOL;
-  if (ly_is_procedure (proc))
-    stil = scm_apply_0 (proc, scm_list_n (this->self_scm (), SCM_UNDEFINED));
+  SCM stil = get_property ("stencil");
 
+  Stencil retval;
   if (Stencil *m = unsmob_stencil (stil))
     {
+      retval = *m;
       if (to_boolean (get_property ("transparent")))
-       stil = Stencil (m->extent_box (), SCM_EOL).smobbed_copy ();
+       retval = Stencil (m->extent_box (), SCM_EOL);
       else
        {
          SCM expr = m->expr ();
          if (point_and_click_global)
            expr = scm_list_3 (ly_symbol2scm ("grob-cause"), self_scm (), expr);
-         
-         stil = Stencil (m->extent_box (), expr).smobbed_copy ();
+
+         retval = Stencil (m->extent_box (), expr);
        }
 
       /* color support... see interpret_stencil_expression () for more... */
@@ -256,11 +246,12 @@ Grob::get_uncached_stencil () const
                                 color,
                                 m->expr ());
 
-         stil = Stencil (m->extent_box (), expr).smobbed_copy ();
+         retval = Stencil (m->extent_box (), expr);
        }
+
     }
 
-  return stil;
+  return retval;
 }
 
 /*
@@ -277,14 +268,6 @@ Grob::get_system () const
   return 0;
 }
 
-void
-Grob::add_dependency (Grob *e)
-{
-  if (e)
-    Pointer_group_interface::add_grob (this, ly_symbol2scm ("dependencies"), e);
-  else
-    programming_error ("null dependency added");
-}
 
 void
 Grob::handle_broken_dependencies ()
@@ -297,19 +280,19 @@ Grob::handle_broken_dependencies ()
     /* THIS, SP is the original spanner.  We use a special function
        because some Spanners have enormously long lists in their
        properties, and a special function fixes FOO  */
-    for (SCM s = mutable_property_alist_; scm_is_pair (s); s = scm_cdr (s))
-      sp->substitute_one_mutable_property (scm_caar (s), scm_cdar (s));
-
+    {
+      for (SCM s = object_alist_; scm_is_pair (s); s = scm_cdr (s))
+       sp->substitute_one_mutable_property (scm_caar (s), scm_cdar (s));
+    }
   System *system = get_system ();
 
   if (is_live ()
-      && system && common_refpoint (system, X_AXIS)
+      && system
+      && common_refpoint (system, X_AXIS)
       && common_refpoint (system, Y_AXIS))
-    substitute_mutable_properties (system
-                                  ? system->self_scm () : SCM_UNDEFINED,
-                                  mutable_property_alist_);
+    substitute_object_links (system->self_scm (), object_alist_);
   else if (dynamic_cast<System *> (this))
-    substitute_mutable_properties (SCM_UNDEFINED, mutable_property_alist_);
+    substitute_object_links (SCM_UNDEFINED, object_alist_);
   else
     /* THIS element is `invalid'; it has been removed from all
        dependencies, so let's junk the element itself.
@@ -331,7 +314,10 @@ Grob::suicide ()
     return;
 
   mutable_property_alist_ = SCM_EOL;
+  object_alist_ = SCM_EOL;
+  property_callbacks_ = SCM_EOL;
   immutable_property_alist_ = SCM_EOL;
+  interfaces_ = SCM_EOL;
 
   set_extent (SCM_EOL, Y_AXIS);
   set_extent (SCM_EOL, X_AXIS);
@@ -350,12 +336,12 @@ void
 Grob::handle_prebroken_dependencies ()
 {
   /* Don't do this in the derived method, since we want to keep access to
-     mutable_property_alist_ centralized.  */
+     object_alist_ centralized.  */
   if (original_)
     {
       Item *it = dynamic_cast<Item *> (this);
-      substitute_mutable_properties (scm_int2num (it->break_status_dir ()),
-                                    original_->mutable_property_alist_);
+      substitute_object_links (scm_from_int (it->break_status_dir ()),
+                              original_->object_alist_);
     }
 }
 
@@ -401,8 +387,8 @@ Grob::get_offset (Axis a) const
   while (dim_cache_[a].offsets_left_)
     {
       int l = --me->dim_cache_[a].offsets_left_;
-      SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, scm_int2num (l));
-      SCM retval = scm_call_2 (cb, self_scm (), scm_int2num (a));
+      SCM cb = scm_list_ref (dim_cache_[a].offset_callbacks_, scm_from_int (l));
+      SCM retval = scm_call_2 (cb, self_scm (), scm_from_int (a));
 
       Real r = scm_to_double (retval);
       if (isinf (r) || isnan (r))
@@ -449,7 +435,7 @@ Grob::extent (Grob *refp, Axis a) const
     ;
   else if (ly_is_procedure (d->dimension_callback_)
           && d->dimension_ == SCM_EOL)
-    d->dimension_ = scm_call_2 (d->dimension_callback_, self_scm (), scm_int2num (a));
+    d->dimension_ = scm_call_2 (d->dimension_callback_, self_scm (), scm_from_int (a));
   else
     return ext;
 
@@ -458,9 +444,9 @@ Grob::extent (Grob *refp, Axis a) const
 
   ext = ly_scm2interval (d->dimension_);
 
-  SCM extra = get_property (a == X_AXIS
-                           ? "extra-X-extent"
-                           : "extra-Y-extent");
+  SCM extra = (a == X_AXIS)
+    ? get_property ("extra-X-extent")
+    : get_property ("extra-Y-extent");
 
   /* Signs ?  */
   if (scm_is_pair (extra))
@@ -469,9 +455,10 @@ Grob::extent (Grob *refp, Axis a) const
       ext[SMALLER] += scm_to_double (scm_car (extra));
     }
 
-  extra = get_property (a == X_AXIS
-                       ? "minimum-X-extent"
-                       : "minimum-Y-extent");
+  extra = (a == X_AXIS)
+    ? get_property ("minimum-X-extent")
+    : get_property ("minimum-Y-extent");
+
   if (scm_is_pair (extra))
     ext.unite (Interval (scm_to_double (scm_car (extra)),
                         scm_to_double (scm_cdr (extra))));
@@ -532,7 +519,7 @@ Grob::name () const
   SCM meta = get_property ("meta");
   SCM nm = scm_assoc (ly_symbol2scm ("name"), meta);
   nm = (scm_is_pair (nm)) ? scm_cdr (nm) : SCM_EOL;
-  return scm_is_symbol (nm) ? ly_symbol2string (nm) : classname (this);
+  return scm_is_symbol (nm) ? ly_symbol2string (nm) : this->class_name ();
 }
 
 void
@@ -576,26 +563,24 @@ Grob::set_parent (Grob *g, Axis a)
   dim_cache_[a].parent_ = g;
 }
 
-MAKE_SCHEME_CALLBACK (Grob, fixup_refpoint, 1);
-SCM
-Grob::fixup_refpoint (SCM smob)
+void
+Grob::fixup_refpoint ()
 {
-  Grob *me = unsmob_grob (smob);
   for (int a = X_AXIS; a < NO_AXES; a++)
     {
       Axis ax = (Axis)a;
-      Grob *parent = me->get_parent (ax);
+      Grob *parent = get_parent (ax);
 
       if (!parent)
        continue;
 
-      if (parent->get_system () != me->get_system () && me->get_system ())
+      if (parent->get_system () != get_system () && get_system ())
        {
-         Grob *newparent = parent->find_broken_piece (me->get_system ());
-         me->set_parent (newparent, ax);
+         Grob *newparent = parent->find_broken_piece (get_system ());
+         set_parent (newparent, ax);
        }
 
-      if (Item *i = dynamic_cast<Item *> (me))
+      if (Item *i = dynamic_cast<Item *> (this))
        {
          Item *parenti = dynamic_cast<Item *> (parent);
 
@@ -605,12 +590,11 @@ Grob::fixup_refpoint (SCM smob)
              if (my_dir != parenti->break_status_dir ())
                {
                  Item *newparent = parenti->find_prebroken_piece (my_dir);
-                 me->set_parent (newparent, ax);
+                 set_parent (newparent, ax);
                }
            }
        }
     }
-  return smob;
 }
 
 void
@@ -629,66 +613,17 @@ Grob::warning (String s) const
 void
 Grob::programming_error (String s) const
 {
-  s = _f ("programming error: %s", s);
-  message (s);
-}
-
-/****************************************************
-  SMOB funcs
-****************************************************/
-
-IMPLEMENT_SMOBS (Grob);
-IMPLEMENT_DEFAULT_EQUAL_P (Grob);
-
-SCM
-Grob::mark_smob (SCM ses)
-{
-  Grob *s = (Grob *) SCM_CELL_WORD_1 (ses);
-  scm_gc_mark (s->immutable_property_alist_);
-  scm_gc_mark (s->key_->self_scm ());
-  for (int a = 0; a < 2; a++)
-    {
-      scm_gc_mark (s->dim_cache_[a].offset_callbacks_);
-      scm_gc_mark (s->dim_cache_[a].dimension_);
-      scm_gc_mark (s->dim_cache_[a].dimension_callback_);
-
-      /* Do not mark the parents.  The pointers in the mutable
-        property list form two tree like structures (one for X
-        relations, one for Y relations).  Marking these can be done
-        in limited stack space.  If we add the parents, we will jump
-        between X and Y in an erratic manner, leading to much more
-        recursion depth (and core dumps if we link to pthreads).  */
-    }
-
-  if (s->original_)
-    scm_gc_mark (s->original_->self_scm ());
-
-  if (s->pscore_)
-    scm_gc_mark (s->pscore_->self_scm ());
-
-  s->do_derived_mark ();
-  return s->mutable_property_alist_;
-}
-
-int
-Grob::print_smob (SCM s, SCM port, scm_print_state *)
-{
-  Grob *sc = (Grob *) SCM_CELL_WORD_1 (s);
-
-  scm_puts ("#<Grob ", port);
-  scm_puts ((char *) sc->name ().to_str0 (), port);
+  SCM cause = self_scm ();
+  while (Grob *g = unsmob_grob (cause))
+    cause = g->get_property ("cause");
 
-  /* Do not print properties, that is too much hassle.  */
-  scm_puts (" >", port);
-  return 1;
-}
+  s = _f ("programming error: %s", s);
 
-SCM
-Grob::do_derived_mark () const
-{
-  return SCM_EOL;
+  if (Music *m = unsmob_music (cause))
+    m->origin ()->message (s);
+  else
+    ::message (s);
 }
-
 void
 Grob::discretionary_processing ()
 {
@@ -697,9 +632,7 @@ Grob::discretionary_processing ()
 bool
 Grob::internal_has_interface (SCM k)
 {
-  SCM ifs = get_property ("interfaces");
-
-  return scm_c_memq (k, ifs) != SCM_BOOL_F;
+  return scm_c_memq (k, interfaces_) != SCM_BOOL_F;
 }
 
 Grob *
@@ -741,8 +674,6 @@ ly_grobs2scm (Link_array<Grob> a)
   return s;
 }
 
-IMPLEMENT_TYPE_P (Grob, "ly:grob?");
-
 ADD_INTERFACE (Grob, "grob-interface",
               "A grob represents a piece of music notation\n"
               "\n"
@@ -771,14 +702,41 @@ ADD_INTERFACE (Grob, "grob-interface",
               "Mutable properties are variables that are specific to one grob. Typically, "
               "lists of other objects, or results from computations are stored in"
               "mutable properties: every call to set-grob-property (or its C++ equivalent) "
-              "sets a mutable property. ",
-              "X-offset-callbacks Y-offset-callbacks X-extent-callback stencil cause "
-              "Y-extent-callback print-function extra-offset spacing-procedure "
-              "context staff-symbol interfaces dependencies X-extent Y-extent extra-X-extent "
-              "meta layer before-line-breaking-callback "
-              "color "
+              "sets a mutable property. "
+              "\n\n"
+              "The properties @code{after-line-breaking} and @code{before-line-breaking} "
+              "are dummies that are not user-serviceable. "
+
+              ,
+
+              /* properties */
+              "X-extent "
+              "X-extent-callback "
+              "X-offset-callbacks "
+              "Y-extent "
+              "Y-extent-callback "
+              "Y-offset-callbacks "
+              "after-line-breaking "
               "axis-group-parent-X "
               "axis-group-parent-Y "
-              "after-line-breaking-callback extra-Y-extent minimum-X-extent "
-              "minimum-Y-extent transparent tweak-count tweak-rank");
+              "before-line-breaking "
+              "callbacks "
+              "cause "
+              "color "
+              "context "
+              "extra-X-extent "
+              "extra-Y-extent "
+              "extra-offset "
+              "interfaces "
+              "layer "
+              "meta "
+              "minimum-X-extent "
+              "minimum-Y-extent "
+              "print-function "
+              "spacing-procedure "
+              "staff-symbol "
+              "stencil "
+              "transparent"
+              );
+