This patch cleans up the engraver part of page-turning. I got rid of all
the clutter in paper-column-engraver in favour of doing things in
page-turn-engraver.

The main change algorithmically is that I defer everything until the
finalize() step. That is, during interpretation I just build up lists of
potential breaks, manual breaks and volta repeats. It probably makes
things a tiny bit faster since the algorithm for revoking turns during
repeats is now linear. More importantly, it solves a problem where the
old engraver would override manual turns.

Here's the ChangeLog, and I promise I will remember the patch...

2006-09-09  Joe Neeman  <[EMAIL PROTECTED]>

        * scm/define-context-properties.scm
        (all-internal-translation-properties): remove properties that
        were used to communicate page-turn stuff to the paper-column
        engraver.

        * lily/lily-guile.cc (robust_scm2string): new function

        * lily/paper-column-engraver.cc: Clean up page turn stuff

        * lily/page-turn-engraver.cc: Re-write the page turn logic here
        instead of cluttering up paper-column-engraver.cc
Index: ChangeLog
===================================================================
RCS file: /sources/lilypond/lilypond/ChangeLog,v
retrieving revision 1.5298
diff -u -r1.5298 ChangeLog
--- ChangeLog	7 Sep 2006 21:30:59 -0000	1.5298
+++ ChangeLog	8 Sep 2006 23:52:00 -0000
@@ -1,3 +1,17 @@
+2006-09-09  Joe Neeman  <[EMAIL PROTECTED]>
+
+	* scm/define-context-properties.scm
+	(all-internal-translation-properties): remove properties that
+	were used to communicate page-turn stuff to the paper-column
+	engraver.
+
+	* lily/lily-guile.cc (robust_scm2string): new function
+
+	* lily/paper-column-engraver.cc: Clean up page turn stuff
+
+	* lily/page-turn-engraver.cc: Re-write the page turn logic here
+	instead of cluttering up paper-column-engraver.cc
+
 2006-09-08  Joe Neeman  <[EMAIL PROTECTED]>
 
 	* Documentation/user/page.itely: update page breaking documentation
Index: lily/lily-guile.cc
===================================================================
RCS file: /sources/lilypond/lilypond/lily/lily-guile.cc,v
retrieving revision 1.246
diff -u -r1.246 lily-guile.cc
--- lily/lily-guile.cc	4 Sep 2006 05:51:14 -0000	1.246
+++ lily/lily-guile.cc	8 Sep 2006 23:52:00 -0000
@@ -653,6 +653,14 @@
   return o;
 }
 
+string
+robust_scm2string (SCM k, string s)
+{
+  if (scm_is_string (k))
+    s = ly_scm2string (k);
+  return s;
+}
+
 int
 robust_scm2int (SCM k, int o)
 {
Index: lily/page-turn-engraver.cc
===================================================================
RCS file: /sources/lilypond/lilypond/lily/page-turn-engraver.cc,v
retrieving revision 1.1
diff -u -r1.1 page-turn-engraver.cc
--- lily/page-turn-engraver.cc	8 Aug 2006 10:01:22 -0000	1.1
+++ lily/page-turn-engraver.cc	8 Sep 2006 23:52:00 -0000
@@ -13,8 +13,58 @@
 #include "grob.hh"
 #include "international.hh"
 #include "moment.hh"
+#include "paper-column.hh"
+#include "stream-event.hh"
 #include "warn.hh"
 
+#include "translator.icc"
+
+class Page_turn_event {
+public:
+  SCM permission_;
+  Real penalty_;
+  Interval_t<Rational> duration_;
+
+  Page_turn_event (Rational start, Rational end, SCM perm, Real pen)
+  {
+    duration_[LEFT] = start;
+    duration_[RIGHT] = end;
+    permission_ = perm;
+    penalty_ = pen;
+  }
+
+  /* Suppose we have decided on a possible page turn, only to change
+     out mind later (for example, if there is a volta repeat and it
+     would be difficult to turn the page back). Then we need to
+     re-penalize a region of the piece. Depending on how the events
+     intersect, we may have to split it into as many as 3 pieces.
+  */
+  vector<Page_turn_event> penalize (Page_turn_event const &penalty)
+  {
+    Interval_t<Rational> intersect = intersection (duration_, penalty.duration_);
+    vector<Page_turn_event> ret;
+
+    if (intersect.is_empty ())
+      {
+	ret.push_back (*this);
+	return ret;
+      }
+
+    Real new_pen = max (penalty_, penalty.penalty_);
+
+    if (duration_[LEFT] < penalty.duration_[LEFT])
+      ret.push_back (Page_turn_event (duration_[LEFT], penalty.duration_[LEFT], permission_, penalty_));
+
+    if (penalty.permission_ != SCM_EOL)
+      ret.push_back (Page_turn_event (intersect[LEFT], intersect[RIGHT], permission_, new_pen));
+
+    if (penalty.duration_[RIGHT] < duration_[RIGHT])
+      ret.push_back (Page_turn_event (penalty.duration_[RIGHT], duration_[LEFT], permission_, penalty_));
+
+    return ret;
+  }
+};
+
 class Page_turn_engraver : public Engraver
 {
   Moment rest_begin_;
@@ -22,14 +72,28 @@
   Moment note_end_;
   Rational repeat_begin_rest_length_;
 
+  vector<Page_turn_event> forced_breaks_;
+  vector<Page_turn_event> automatic_breaks_;
+  vector<Page_turn_event> repeat_penalties_;
+
+  /* the next 3 are in sync (ie. same number of elements, etc.) */
+  vector<Rational> breakable_moments_;
+  vector<Grob*> breakable_columns_;
+  vector<bool> special_barlines_;
+
+  SCM max_permission (SCM perm1, SCM perm2);
   Real penalty (Rational rest_len);
+  Grob *breakable_column (Page_turn_event const &);
 
 protected:
+  DECLARE_TRANSLATOR_LISTENER (break);
   DECLARE_ACKNOWLEDGER (note_head);
 
 public:
   TRANSLATOR_DECLARATIONS (Page_turn_engraver);
   void stop_translation_timestep ();
+  void start_translation_timestep ();
+  void finalize ();
 };
 
 Page_turn_engraver::Page_turn_engraver ()
@@ -40,10 +104,32 @@
   note_end_ = 0;
 }
 
+Grob*
+Page_turn_engraver::breakable_column (Page_turn_event const &brk)
+{
+  vsize start = lower_bound (breakable_moments_, brk.duration_[LEFT], less<Rational> ());
+  vsize end = upper_bound (breakable_moments_, brk.duration_[RIGHT], less<Rational> ());
+
+  if (start == breakable_moments_.size ())
+    return NULL;
+  if (end == breakable_moments_.size () || breakable_moments_[end] > brk.duration_[RIGHT])
+    {
+      if (end == 0)
+	return NULL;
+      end--;
+    }
+
+  for (vsize i = end + 1; i-- > start;)
+    if (special_barlines_[i])
+      return breakable_columns_[i];
+
+  return breakable_columns_[end];
+}
+
 Real
 Page_turn_engraver::penalty (Rational rest_len)
 {
-  Rational min_turn = robust_scm2moment (get_property ("minPageTurnLength"), Moment (1)).main_part_;
+  Rational min_turn = robust_scm2moment (get_property ("minimumPageTurnLength"), Moment (1)).main_part_;
 
   return (rest_len < min_turn) ? infinity_f : 0;
 }
@@ -62,10 +148,9 @@
     {
       Real pen = penalty ((now_mom () - rest_begin_).main_part_);
       if (!isinf (pen))
-	{
-          SCM val = scm_cons (rest_begin_.smobbed_copy (), scm_from_double (pen));
-          context ()->get_score_context ()->set_property ("allowPageTurn", val);
-        }
+	  automatic_breaks_.push_back (Page_turn_event (rest_begin_.main_part_,
+							now_mom ().main_part_,
+							ly_symbol2scm ("allow"), 0));
     }
 
   if (rest_begin_ <= repeat_begin_)
@@ -73,9 +158,59 @@
   note_end_ = now_mom () + Moment (Duration (dur_log, dot_count).get_length ());
 }
 
+IMPLEMENT_TRANSLATOR_LISTENER (Page_turn_engraver, break);
+void
+Page_turn_engraver::listen_break (Stream_event *ev)
+{
+  string name = ly_scm2string (scm_symbol_to_string (ev->get_property ("class")));
+
+  if (name == "page-turn-event")
+    {
+      SCM permission = ev->get_property ("break-permission");
+      Real penalty = robust_scm2double (ev->get_property ("break-penalty"), 0);
+      Rational now = now_mom ().main_part_;
+
+      forced_breaks_.push_back (Page_turn_event (now, now, permission, penalty));
+    }
+}
+
+void
+Page_turn_engraver::start_translation_timestep ()
+{
+  /* What we want to do is to build a list of all the
+     breakable paper columns. In general, paper-columns won't be marked as
+     such until the Paper_column_engraver has done stop_translation_timestep.
+
+     Therefore, we just grab /all/ paper columns (in the
+     stop_translation_timestep, since they're not created here yet)
+     and remove the non-breakable ones at the beginning of the following
+     timestep.
+  */
+
+  if (breakable_columns_.size () && !Paper_column::is_breakable (breakable_columns_.back ()))
+    {
+      breakable_columns_.pop_back ();
+      breakable_moments_.pop_back ();
+      special_barlines_.pop_back ();
+    }
+}
+
 void
 Page_turn_engraver::stop_translation_timestep ()
 {
+  Grob *pc = unsmob_grob (get_property ("currentCommandColumn"));
+
+  if (pc)
+    {
+      breakable_columns_.push_back (pc);
+      breakable_moments_.push_back (now_mom ().main_part_);
+
+      SCM bar_scm = get_property ("whichBar");
+      string bar = robust_scm2string (bar_scm, "");
+
+      special_barlines_.push_back (bar != "" && bar != "|");
+    }
+
   /* C&P from Repeat_acknowledge_engraver */
   SCM cs = get_property ("repeatCommands");
   bool start = false;
@@ -92,17 +227,20 @@
 
   if (end && repeat_begin_.main_part_ >= Moment (0))
     {
+      Rational now = now_mom ().main_part_;
       Real pen = penalty ((now_mom () - rest_begin_).main_part_ + repeat_begin_rest_length_);
       Moment *m = unsmob_moment (get_property ("minimumRepeatLengthForPageTurn"));
       if (m && *m > (now_mom () - repeat_begin_))
 	pen = infinity_f;
-      if (pen > 0)
-	{
-	  SCM val = scm_cons (repeat_begin_.smobbed_copy (), scm_from_double (pen));
-	  context ()->get_score_context ()->set_property ("revokePageTurns", val);
-	}
+
+      if (pen == infinity_f)
+	repeat_penalties_.push_back (Page_turn_event (repeat_begin_.main_part_, now, SCM_EOL, -infinity_f));
+      else
+	repeat_penalties_.push_back (Page_turn_event (repeat_begin_.main_part_, now, ly_symbol2scm ("allow"), pen));
+
       repeat_begin_ = Moment (-1);
     }
+
   if (start)
     {
       repeat_begin_ = now_mom ();
@@ -111,15 +249,92 @@
   rest_begin_ = note_end_;
 }
 
-#include "translator.icc"
+/* return the most permissive symbol (where force is the most permissive and
+   forbid is the least
+*/
+SCM
+Page_turn_engraver::max_permission (SCM perm1, SCM perm2)
+{
+  if (perm1 == SCM_EOL)
+    return perm2;
+  if (perm1 == ly_symbol2scm ("allow") && perm2 == ly_symbol2scm ("force"))
+    return perm2;
+  return perm1;
+}
+
+void
+Page_turn_engraver::finalize ()
+{
+  vsize rep_index = 0;
+  vector<Page_turn_event> auto_breaks;
+
+  /* filter the automatic breaks through the repeat penalties */
+  for (vsize i = 0; i < automatic_breaks_.size (); i++)
+    {
+      Page_turn_event &brk = automatic_breaks_[i];
+
+      /* find the next applicable repeat penalty */
+      for (;
+	   rep_index < repeat_penalties_.size ()
+	     && repeat_penalties_[rep_index].duration_[RIGHT] <= brk.duration_[LEFT];
+	   rep_index++)
+	;
+
+      if (rep_index >= repeat_penalties_.size ()
+	  || brk.duration_[RIGHT] <= repeat_penalties_[rep_index].duration_[LEFT])
+	auto_breaks.push_back (brk);
+      else
+	{
+	  vector<Page_turn_event> split = brk.penalize (repeat_penalties_[rep_index]);
+
+	  /* it's possible that the last of my newly-split events overlaps the next repeat_penalty,
+	     in which case we need to refilter that event */
+	  if (rep_index < repeat_penalties_.size () - 1
+	      && split.size ()
+	      && split.back ().duration_[RIGHT] > repeat_penalties_[rep_index+1].duration_[LEFT])
+	    {
+	      automatic_breaks_[i] = split.back ();
+	      split.pop_back ();
+	      i--;
+	    }
+	  auto_breaks.insert (auto_breaks.end (), split.begin (), split.end ());
+	}
+    }
+
+  /* apply the automatic breaks */
+  for (vsize i = 0; i < auto_breaks.size (); i++)
+    {
+      Page_turn_event const &brk = auto_breaks[i];
+      Grob *pc = breakable_column (auto_breaks[i]);
+      if (pc)
+	{
+	  SCM perm = max_permission (pc->get_property ("page-turn-permission"), brk.permission_);
+	  Real pen = min (robust_scm2double (pc->get_property ("page-turn-penalty"), infinity_f), brk.penalty_);
+	  pc->set_property ("page-turn-permission", perm);
+	  pc->set_property ("page-turn-penalty", scm_from_double (pen));
+	}
+    }
+
+  /* apply the manual breaks */
+  for (vsize i = 0; i < forced_breaks_.size (); i++)
+    {
+      Page_turn_event const &brk = forced_breaks_[i];
+      Grob *pc = breakable_column (forced_breaks_[i]);
+      if (pc)
+	{
+	  pc->set_property ("page-turn-permission", brk.permission_);
+	  pc->set_property ("page-turn-penalty", scm_from_double (brk.penalty_));
+	}
+    }
+}
 
 ADD_ACKNOWLEDGER (Page_turn_engraver, note_head);
 ADD_TRANSLATOR (Page_turn_engraver,
                 /* doc */ "Decide where page turns are allowed to go",
                 /* create */ "",
                 /* accept */ "",
-                /* read */ "",
-                /* write */
-		"allowPageTurn "
-		"revokePageTurns "
+                /* read */
+		"minimumPageTurnLength "
+		"minimumRepeatLengthForPageTurn ",
+                /* write */ ""
 		);
Index: lily/paper-column-engraver.cc
===================================================================
RCS file: /sources/lilypond/lilypond/lily/paper-column-engraver.cc,v
retrieving revision 1.22
diff -u -r1.22 paper-column-engraver.cc
--- lily/paper-column-engraver.cc	26 Aug 2006 22:18:26 -0000	1.22
+++ lily/paper-column-engraver.cc	8 Sep 2006 23:52:00 -0000
@@ -28,8 +28,6 @@
   musical_column_ = 0;
   breaks_ = 0;
   system_ = 0;
-  last_special_barline_column_ = 0;
-  last_breakable_column_ = 0;
   first_ = true;
 }
 
@@ -171,56 +169,6 @@
     }
 }
 
-/* return either
-   - the last column with a special (ie. not "|" or "") barline
-   - the last column
-   after the given moment
-*/
-Paper_column*
-Paper_column_engraver::find_turnable_column (Moment after_this)
-{
-  if (last_special_barline_column_)
-    {
-      Moment m = *unsmob_moment (last_special_barline_column_->get_property ("when"));
-      if (m >= after_this)
-	return last_special_barline_column_;
-    }
-  if (last_breakable_column_)
-    {
-      Moment m = *unsmob_moment (last_breakable_column_->get_property ("when"));
-      if (m >= after_this)
-	return last_breakable_column_;
-    }
-  return 0;
-}
-
-void
-Paper_column_engraver::revoke_page_turns (Moment after_this, Real new_penalty)
-{
-  if (!page_turnable_columns_.size ())
-    return;
-
-  for (vsize i = page_turnable_columns_.size () - 1; i--;)
-    {
-      Paper_column *col = page_turnable_columns_[i];
-      Moment mom = *unsmob_moment (col->get_property ("when"));
-      if (mom >= after_this)
-	{
-	  if (isinf (new_penalty))
-	    {
-	      col->del_property ( ly_symbol2scm ("page-turn-permission"));
-	      page_turnable_columns_.erase (page_turnable_columns_.begin () + i);
-	    }
-	  else
-	    {
-	      Real prev_pen = robust_scm2double (col->get_property ("page-turn-penalty"), 0);
-	      if (new_penalty > prev_pen)
-		col->set_property ("page-turn-penalty", scm_from_double (new_penalty));
-	    }
-	}
-    }
-}
-
 void
 Paper_column_engraver::stop_translation_timestep ()
 {
@@ -245,51 +193,12 @@
   else if (Paper_column::is_breakable (command_column_))
     {
       breaks_++;
-      last_breakable_column_ = command_column_;
-
-      SCM which_bar = get_property ("whichBar");
-      if (scm_is_string (which_bar))
-	{
-	  string bar = ly_scm2string (which_bar);
-	  if (bar != "" && bar != "|")
-	    last_special_barline_column_ = command_column_;
-	}
 
       if (! (breaks_%8))
 	progress_indication ("[" + to_string (breaks_) + "]");
     }
 
-  SCM page_br = get_property ("allowPageTurn");
-  if (scm_is_pair (page_br) && last_breakable_column_)
-    {
-      SCM pen = scm_cdr (page_br);
-      Moment *m = unsmob_moment (scm_car (page_br));
-      if (m)
-	{
-	  Paper_column *turn = find_turnable_column (*m);
-	  if (turn)
-	    {
-	      turn->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
-	      turn->set_property ("page-turn-penalty", pen);
-	      page_turnable_columns_.push_back (turn);
-	    }
-	}
-    }
-
-  /* The page-turn-engraver is allowed to change its mind and revoke previously-allowed
-     page turns (for example if there is a volta repeat where a turn is inconvenient) */
-  SCM revokes = get_property ("revokePageTurns");
-  if (scm_is_pair (revokes))
-    {
-      Moment *start = unsmob_moment (scm_car (revokes));
-      Real pen = robust_scm2double (scm_cdr (revokes), infinity_f);
-      if (start)
-	revoke_page_turns (*start, pen);
-    }
-
   context ()->get_score_context ()->unset_property (ly_symbol2scm ("forbidBreak"));
-  context ()->get_score_context ()->unset_property (ly_symbol2scm ("allowPageTurn"));
-  context ()->get_score_context ()->unset_property (ly_symbol2scm ("revokePageTurns"));
 
   first_ = false;
   break_events_.clear ();
@@ -326,13 +235,9 @@
 		/* accept */ "break-event",
 		/* read */
                 "forbidBreak "
-                "allowPageTurn "
-		"revokePageTurns "
 		,
 		/* write */
                 "forbidBreak "
-                "allowPageTurn "
-		"revokePageTurns "
 		"currentCommandColumn "
 		"currentMusicalColumn "
 		);
Index: lily/include/lily-guile.hh
===================================================================
RCS file: /sources/lilypond/lilypond/lily/include/lily-guile.hh,v
retrieving revision 1.176
diff -u -r1.176 lily-guile.hh
--- lily/include/lily-guile.hh	4 Sep 2006 05:51:14 -0000	1.176
+++ lily/include/lily-guile.hh	8 Sep 2006 23:52:00 -0000
@@ -64,6 +64,7 @@
 Drul_array<bool> robust_scm2booldrul (SCM, Drul_array<bool>);
 Interval robust_scm2interval (SCM, Drul_array<Real>);
 Offset robust_scm2offset (SCM, Offset);
+string robust_scm2string (SCM, string);
 
 SCM ly_quote_scm (SCM s);
 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol);
Index: lily/include/paper-column-engraver.hh
===================================================================
RCS file: /sources/lilypond/lilypond/lily/include/paper-column-engraver.hh,v
retrieving revision 1.11
diff -u -r1.11 paper-column-engraver.hh
--- lily/include/paper-column-engraver.hh	8 Aug 2006 10:01:22 -0000	1.11
+++ lily/include/paper-column-engraver.hh	8 Sep 2006 23:52:01 -0000
@@ -44,9 +44,6 @@
   bool first_;
   Moment last_moment_;
 
-  Paper_column *last_special_barline_column_;
-  Paper_column *last_breakable_column_;
-  vector<Paper_column*> page_turnable_columns_;
 public:
 };
 
Index: scm/define-context-properties.scm
===================================================================
RCS file: /sources/lilypond/lilypond/scm/define-context-properties.scm,v
retrieving revision 1.84
diff -u -r1.84 define-context-properties.scm
--- scm/define-context-properties.scm	23 Aug 2006 21:19:36 -0000	1.84
+++ scm/define-context-properties.scm	8 Sep 2006 23:52:01 -0000
@@ -39,8 +39,6 @@
 				 "If true, then the accidentals are aligned in bass figure context.")
 
      (allowBeamBreak ,boolean? "If true allow line breaks for beams over bar lines.")
-     (allowPageTurn ,pair? "In the form (moment-start . penalty). Allow a page turn
-at the most recent breakpoint if it was after moment-start.")
      (associatedVoice ,string? "Name of the
 @code{Voice} that has the melody for this @code{Lyrics} line.")
      (autoBeamSettings ,list? "Specifies
@@ -313,6 +311,9 @@
      (midiMaximumVolume ,number? "Analogous to @code{midiMinimumVolume}.")
      (minimumFret ,number? "The tablature auto string-selecting mechanism
 selects the highest string with a fret at least @code{minimumFret}")
+     (minimumPageTurnLength ,ly:moment? "Minimum length of a rest for a page turn to be allowed")
+     (minimumRepeatLengthForPageTurn ,ly:moment? "Minimum length of a repeated section for a page
+turn to be allowed within that section")
      (minimumVerticalExtent ,number-pair? "minimum vertical extent, same
 format as @var{verticalExtent}")
      (output ,ly:music-output? "The output produced by a score-level translator during music interpretation")
@@ -346,7 +347,6 @@
      (restNumberThreshold ,number?
 			  "If a multimeasure rest has more measures
 than this, a number is printed. ")
-     (revokePageTurns ,pair? "Signals to the paper-column-engraver to revoke (or increase the penalties for) all the page turns within a time interval. Used to disable page turns that occur within an unturnable volta repeat.")
      (shapeNoteStyles ,vector? "Vector of symbols, listing style for each note
 head relative to the tonic (qv.) of the scale.")
      (shortInstrumentName ,markup? "See @code{instrument}")
_______________________________________________
lilypond-devel mailing list
lilypond-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/lilypond-devel

Reply via email to