]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/accidental-placement.cc
2003 -> 2004
[lilypond.git] / lily / accidental-placement.cc
index 292f9935512f1195fb6458a994338e7f07dc387b..10ebcbc66d9500ab11fd72fe4ecbb748f42d9bbf 100644 (file)
@@ -3,7 +3,7 @@ accidental-placement.cc --  implement Accidental_placement
 
 source file of the GNU LilyPond music typesetter
 
-(c) 2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+(c) 2002--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 
  */
 
@@ -20,22 +20,6 @@ source file of the GNU LilyPond music typesetter
 #include "note-collision.hh"
 #include "accidental-interface.hh"
 
-/*
-  Hmm. why not group-extent? 
- */
-MAKE_SCHEME_CALLBACK(Accidental_placement,extent_callback, 2);
-SCM
-Accidental_placement::extent_callback(SCM s, SCM axis)
-{
-  Grob * me =unsmob_grob (s);
-  Axis a = Axis (gh_scm2int (axis));
-
-  assert (a == X_AXIS);
-
-  SCM w = position_accidentals (me);
-  return w;
-}
-
 MAKE_SCHEME_CALLBACK(Accidental_placement,alignment_callback, 2);
 SCM
 Accidental_placement::alignment_callback(SCM s, SCM )
@@ -43,17 +27,16 @@ Accidental_placement::alignment_callback(SCM s, SCM )
   Grob * me =unsmob_grob (s);
 
   Grob * par = me->get_parent (X_AXIS);
-  if (!to_boolean (par->get_grob_property ("alignment-done")))
+  if (!to_boolean (par->get_grob_property ("positioning-done")))
     {
-      par->set_grob_property ("alignment-done", SCM_BOOL_T);
+      par->set_grob_property ("positioning-done", SCM_BOOL_T);
       position_accidentals (par);
     }
 
-  return gh_int2scm (0);
+  return scm_int2num (0);
 }
 
 
-
 void
 Accidental_placement::add_accidental (Grob* me, Grob* a)
 {
@@ -70,10 +53,10 @@ Accidental_placement::add_accidental (Grob* me, Grob* a)
 
   Pitch *p= unsmob_pitch (mcause->get_mus_property ("pitch"));
 
-  int n = p->notename_i_;
+  int n = p->get_notename ();
 
-  SCM accs = me->get_grob_property ("accidentals");
-  SCM key = gh_int2scm (n);
+  SCM accs = me->get_grob_property ("accidental-grobs");
+  SCM key = scm_int2num (n);
   SCM entry = scm_assq (key, accs);
   if (entry == SCM_BOOL_F)
     {
@@ -86,7 +69,7 @@ Accidental_placement::add_accidental (Grob* me, Grob* a)
 
   accs = scm_assq_set_x (accs,  key, entry);
 
-  me->set_grob_property ("accidentals", accs);
+  me->set_grob_property ("accidental-grobs", accs);
 }
 
 /*
@@ -97,7 +80,7 @@ Accidental_placement::split_accidentals (Grob * accs,
                                         Link_array<Grob> *break_reminder,
                                         Link_array<Grob> *real_acc)
 {
-  for (SCM acs =accs->get_grob_property ("accidentals"); gh_pair_p (acs);
+  for (SCM acs =accs->get_grob_property ("accidental-grobs"); gh_pair_p (acs);
        acs =gh_cdr (acs))
     for (SCM s = gh_cdar (acs); gh_pair_p (s); s = gh_cdr (s))
       {
@@ -136,9 +119,9 @@ Accidental_placement::get_relevant_accidental_extent (Grob *me,
       extent.unite (which->elem(i)->extent (item_col, X_AXIS));
     }
 
-  if (!extent.empty_b())
+  if (!extent.is_empty ())
     {
-      Real p = gh_scm2double (me->get_grob_property ("left-padding"));
+      Real p = robust_scm2double (me->get_grob_property ("left-padding"), 0.2);
       extent[LEFT] -= p;
     }
   
@@ -224,8 +207,6 @@ stagger_apes (Link_array<Accidental_placement_entry> *apes)
   
 
 /*
-  Return: width as SCM interval.
-
 
   This routine computes placements of accidentals. During
   add_accidental(), accidentals are already grouped by note, so that
@@ -253,14 +234,29 @@ stagger_apes (Link_array<Accidental_placement_entry> *apes)
 
 */
 
+/*
+  TODO: there should be more space in the following situation
+
+
+    Natural + downstem
+
+    |_       
+    | |    X
+    |_|   | 
+      |   |
+      
+ */
 SCM
 Accidental_placement::position_accidentals (Grob * me)
 {
-  SCM accs = me->get_grob_property ("accidentals");
+  if (!me->live ())
+    return SCM_UNSPECIFIED;
+  
+  SCM accs = me->get_grob_property ("accidental-grobs");
 
   /*
     TODO: there is a bug in this code. If two accs are on the same
-    Y-position, they share an Ape, and will be pritned in overstrike.
+    Y-position, they share an Ape, and will be printed in overstrike.
    */
   Link_array<Accidental_placement_entry> apes;
   for (SCM s = accs; gh_pair_p (s); s =gh_cdr (s))
@@ -384,53 +380,32 @@ Accidental_placement::position_accidentals (Grob * me)
   head_ape-> left_skyline_ = head_skyline;
   head_ape->offset_ = 0.0;
 
-  SCM rs = me->get_grob_property ("right-padding");
-  if (gh_number_p (rs))
-    head_ape->offset_ -= gh_scm2double (rs);
+  head_ape->offset_ -= robust_scm2double ( me->get_grob_property ("right-padding"), 0);
 
   
-  Real padding = 0.2;
-  SCM spad = me->get_grob_property ("padding");
-  if (gh_number_p (spad))
-    padding = gh_scm2double (spad);
-
+  Real padding = robust_scm2double (me->get_grob_property ("padding"),0.2);
 
+  Array<Skyline_entry> left_skyline = head_ape->left_skyline_;
   /*
-    TODO:
-
-    There is a bug in this code: the left_skylines should be
-    accumulated, otherwise the b will collide with bb in
-
-      bb
-    b 
-      n 
-    
+    Add accs entries right-to-left.
    */
-  apes.push (head_ape);
-  for (int i= apes.size () -1 ; i-- > 0;)
+  for (int i= apes.size (); i-- > 0;)
     {
-      Accidental_placement_entry *ape = apes[i];
-      Real d = 0.0;
-      int j = i+1;
-      do {
-       d = - skyline_meshing_distance (ape->right_skyline_,
-                                       apes[j]->left_skyline_);
-
-       if (!isinf(d)
-           || j + 1 == apes.size())
-         break;
-       
-       j = j+ 1;
-      } while (1);
-
-      if (isinf(d))
-       d = 0.0;
-      
-      d -= padding;
-      ape->offset_ += apes[j]->offset_ + d;
-    }
+      Real offset =
+       -skyline_meshing_distance (apes[i]->right_skyline_, left_skyline);
+      if (isinf (offset))
+       offset = (i < apes.size () - 1) ? apes[i+1]->offset_ : 0.0;
+      else
+       offset -= padding;
+
+      apes[i]->offset_ = offset;
+
+      Array<Skyline_entry> new_left_skyline = apes[i]->left_skyline_;
+      heighten_skyline (&new_left_skyline, apes[i]->offset_);
+      merge_skyline (&new_left_skyline, left_skyline, LEFT);
+      left_skyline = new_left_skyline;
+    }      
 
-  apes.pop();
   for (int i = apes.size(); i--;)
     {
       Accidental_placement_entry* ape = apes[i];
@@ -451,9 +426,8 @@ Accidental_placement::position_accidentals (Grob * me)
   for (int i = ape->extents_.size(); i--;)
     right_extent.unite (ape->offset_  + ape->extents_[i][X_AXIS]);
 
-  SCM ls = me->get_grob_property ("left-padding");
-  if (gh_number_p (rs))
-    left_extent[LEFT] -= gh_scm2double (ls);
+  
+  left_extent[LEFT] -= robust_scm2double (me->get_grob_property ("left-padding"), 0);
 
   
   Interval width(left_extent[LEFT], right_extent[RIGHT]);
@@ -464,10 +438,10 @@ Accidental_placement::position_accidentals (Grob * me)
   for (int i = apes.size(); i--;)
     delete apes[i];
 
-  return scm_width;
+  return SCM_UNSPECIFIED;
 }
 
 ADD_INTERFACE(Accidental_placement,
              "accidental-placement-interface",
              "Take care of complex accidental collisions.",
-             "left-padding padding right-padding accidentals alignment-done")
+             "left-padding padding right-padding accidental-grobs positioning-done")