2 page-turn-engraver.cc -- implement Page_turn_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2006 Joe Neeman <joeneeman@gmail.com>
12 #include "duration.hh"
14 #include "international.hh"
16 #include "paper-column.hh"
17 #include "stream-event.hh"
20 #include "translator.icc"
22 class Page_turn_event {
26 Interval_t<Rational> duration_;
28 Page_turn_event (Rational start, Rational end, SCM perm, Real pen)
30 duration_[LEFT] = start;
31 duration_[RIGHT] = end;
36 /* Suppose we have decided on a possible page turn, only to change
37 out mind later (for example, if there is a volta repeat and it
38 would be difficult to turn the page back). Then we need to
39 re-penalize a region of the piece. Depending on how the events
40 intersect, we may have to split it into as many as 3 pieces.
42 vector<Page_turn_event> penalize (Page_turn_event const &penalty)
44 Interval_t<Rational> intersect = intersection (duration_, penalty.duration_);
45 vector<Page_turn_event> ret;
47 if (intersect.is_empty ())
49 ret.push_back (*this);
53 Real new_pen = max (penalty_, penalty.penalty_);
55 if (duration_[LEFT] < penalty.duration_[LEFT])
56 ret.push_back (Page_turn_event (duration_[LEFT], penalty.duration_[LEFT], permission_, penalty_));
58 if (penalty.permission_ != SCM_EOL)
59 ret.push_back (Page_turn_event (intersect[LEFT], intersect[RIGHT], permission_, new_pen));
61 if (penalty.duration_[RIGHT] < duration_[RIGHT])
62 ret.push_back (Page_turn_event (penalty.duration_[RIGHT], duration_[RIGHT], permission_, penalty_));
68 class Page_turn_engraver : public Engraver
73 Rational repeat_begin_rest_length_;
75 vector<Page_turn_event> forced_breaks_;
76 vector<Page_turn_event> automatic_breaks_;
77 vector<Page_turn_event> repeat_penalties_;
79 /* the next 3 are in sync (ie. same number of elements, etc.) */
80 vector<Rational> breakable_moments_;
81 vector<Grob*> breakable_columns_;
82 vector<bool> special_barlines_;
84 SCM max_permission (SCM perm1, SCM perm2);
85 Real penalty (Rational rest_len);
86 Grob *breakable_column (Page_turn_event const &);
89 DECLARE_TRANSLATOR_LISTENER (break);
90 DECLARE_ACKNOWLEDGER (note_head);
93 TRANSLATOR_DECLARATIONS (Page_turn_engraver);
94 void stop_translation_timestep ();
95 void start_translation_timestep ();
99 Page_turn_engraver::Page_turn_engraver ()
101 repeat_begin_ = Moment (-1);
102 repeat_begin_rest_length_ = 0;
108 Page_turn_engraver::breakable_column (Page_turn_event const &brk)
110 vsize start = lower_bound (breakable_moments_, brk.duration_[LEFT], less<Rational> ());
111 vsize end = upper_bound (breakable_moments_, brk.duration_[RIGHT], less<Rational> ());
113 if (start == breakable_moments_.size ())
119 for (vsize i = end + 1; i-- > start;)
120 if (special_barlines_[i])
121 return breakable_columns_[i];
123 return breakable_columns_[end];
127 Page_turn_engraver::penalty (Rational rest_len)
129 Rational min_turn = robust_scm2moment (get_property ("minimumPageTurnLength"), Moment (1)).main_part_;
131 return (rest_len < min_turn) ? infinity_f : 0;
135 Page_turn_engraver::acknowledge_note_head (Grob_info gi)
137 SCM dur_log_scm = gi.grob ()->get_property ("duration-log");
138 if (!scm_is_number (dur_log_scm))
141 int dur_log = scm_to_int (dur_log_scm);
142 int dot_count = robust_scm2int (gi.grob ()->get_property ("dot-count"), 0);
144 if (rest_begin_ < now_mom ())
146 Real pen = penalty ((now_mom () - rest_begin_).main_part_);
148 automatic_breaks_.push_back (Page_turn_event (rest_begin_.main_part_,
149 now_mom ().main_part_,
150 ly_symbol2scm ("allow"), 0));
153 if (rest_begin_ <= repeat_begin_)
154 repeat_begin_rest_length_ = (now_mom () - repeat_begin_).main_part_;
155 note_end_ = now_mom () + Moment (Duration (dur_log, dot_count).get_length ());
158 IMPLEMENT_TRANSLATOR_LISTENER (Page_turn_engraver, break);
160 Page_turn_engraver::listen_break (Stream_event *ev)
162 string name = ly_scm2string (scm_symbol_to_string (ev->get_property ("class")));
164 if (name == "page-turn-event")
166 SCM permission = ev->get_property ("break-permission");
167 Real penalty = robust_scm2double (ev->get_property ("break-penalty"), 0);
168 Rational now = now_mom ().main_part_;
170 forced_breaks_.push_back (Page_turn_event (now, now, permission, penalty));
175 Page_turn_engraver::start_translation_timestep ()
177 /* What we want to do is to build a list of all the
178 breakable paper columns. In general, paper-columns won't be marked as
179 such until the Paper_column_engraver has done stop_translation_timestep.
181 Therefore, we just grab /all/ paper columns (in the
182 stop_translation_timestep, since they're not created here yet)
183 and remove the non-breakable ones at the beginning of the following
187 if (breakable_columns_.size () && !Paper_column::is_breakable (breakable_columns_.back ()))
189 breakable_columns_.pop_back ();
190 breakable_moments_.pop_back ();
191 special_barlines_.pop_back ();
196 Page_turn_engraver::stop_translation_timestep ()
198 Grob *pc = unsmob_grob (get_property ("currentCommandColumn"));
202 breakable_columns_.push_back (pc);
203 breakable_moments_.push_back (now_mom ().main_part_);
205 SCM bar_scm = get_property ("whichBar");
206 string bar = robust_scm2string (bar_scm, "");
208 special_barlines_.push_back (bar != "" && bar != "|");
211 /* C&P from Repeat_acknowledge_engraver */
212 SCM cs = get_property ("repeatCommands");
216 for (; scm_is_pair (cs); cs = scm_cdr (cs))
218 SCM command = scm_car (cs);
219 if (command == ly_symbol2scm ("start-repeat"))
221 else if (command == ly_symbol2scm ("end-repeat"))
225 if (end && repeat_begin_.main_part_ >= Moment (0))
227 Rational now = now_mom ().main_part_;
228 Real pen = penalty ((now_mom () - rest_begin_).main_part_ + repeat_begin_rest_length_);
229 Moment *m = unsmob_moment (get_property ("minimumRepeatLengthForPageTurn"));
230 if (m && *m > (now_mom () - repeat_begin_))
233 if (pen == infinity_f)
234 repeat_penalties_.push_back (Page_turn_event (repeat_begin_.main_part_, now, SCM_EOL, -infinity_f));
236 repeat_penalties_.push_back (Page_turn_event (repeat_begin_.main_part_, now, ly_symbol2scm ("allow"), pen));
238 repeat_begin_ = Moment (-1);
243 repeat_begin_ = now_mom ();
244 repeat_begin_rest_length_ = 0;
246 rest_begin_ = note_end_;
249 /* return the most permissive symbol (where force is the most permissive and
253 Page_turn_engraver::max_permission (SCM perm1, SCM perm2)
255 if (perm1 == SCM_EOL)
257 if (perm1 == ly_symbol2scm ("allow") && perm2 == ly_symbol2scm ("force"))
263 Page_turn_engraver::finalize ()
266 vector<Page_turn_event> auto_breaks;
268 /* filter the automatic breaks through the repeat penalties */
269 for (vsize i = 0; i < automatic_breaks_.size (); i++)
271 Page_turn_event &brk = automatic_breaks_[i];
273 /* find the next applicable repeat penalty */
275 rep_index < repeat_penalties_.size ()
276 && repeat_penalties_[rep_index].duration_[RIGHT] <= brk.duration_[LEFT];
280 if (rep_index >= repeat_penalties_.size ()
281 || brk.duration_[RIGHT] <= repeat_penalties_[rep_index].duration_[LEFT])
282 auto_breaks.push_back (brk);
285 vector<Page_turn_event> split = brk.penalize (repeat_penalties_[rep_index]);
287 /* it's possible that the last of my newly-split events overlaps the next repeat_penalty,
288 in which case we need to refilter that event */
289 if (rep_index < repeat_penalties_.size () - 1
291 && split.back ().duration_[RIGHT] > repeat_penalties_[rep_index+1].duration_[LEFT])
293 automatic_breaks_[i] = split.back ();
297 auto_breaks.insert (auto_breaks.end (), split.begin (), split.end ());
301 /* apply the automatic breaks */
302 for (vsize i = 0; i < auto_breaks.size (); i++)
304 Page_turn_event const &brk = auto_breaks[i];
305 Grob *pc = breakable_column (auto_breaks[i]);
308 SCM perm = max_permission (pc->get_property ("page-turn-permission"), brk.permission_);
309 Real pen = min (robust_scm2double (pc->get_property ("page-turn-penalty"), infinity_f), brk.penalty_);
310 pc->set_property ("page-turn-permission", perm);
311 pc->set_property ("page-turn-penalty", scm_from_double (pen));
315 /* apply the manual breaks */
316 for (vsize i = 0; i < forced_breaks_.size (); i++)
318 Page_turn_event const &brk = forced_breaks_[i];
319 Grob *pc = breakable_column (forced_breaks_[i]);
322 pc->set_property ("page-turn-permission", brk.permission_);
323 pc->set_property ("page-turn-penalty", scm_from_double (brk.penalty_));
328 ADD_ACKNOWLEDGER (Page_turn_engraver, note_head);
329 ADD_TRANSLATOR (Page_turn_engraver,
330 /* doc */ "Decide where page turns are allowed to go",
334 "minimumPageTurnLength "
335 "minimumRepeatLengthForPageTurn ",