2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2006--2011 Joe Neeman <joeneeman@gmail.com>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "engraver.hh"
23 #include "duration.hh"
25 #include "international.hh"
26 #include "paper-column.hh"
27 #include "stream-event.hh"
30 #include "translator.icc"
37 Interval_t<Rational> duration_;
39 Page_turn_event (Rational start, Rational end, SCM perm, Real pen)
41 duration_[LEFT] = start;
42 duration_[RIGHT] = end;
47 /* Suppose we have decided on a possible page turn, only to change
48 out mind later (for example, if there is a volta repeat and it
49 would be difficult to turn the page back). Then we need to
50 re-penalize a region of the piece. Depending on how the events
51 intersect, we may have to split it into as many as 3 pieces.
53 vector<Page_turn_event> penalize (Page_turn_event const &penalty)
55 Interval_t<Rational> intersect = intersection (duration_, penalty.duration_);
56 vector<Page_turn_event> ret;
58 if (intersect.is_empty ())
60 ret.push_back (*this);
64 Real new_pen = max (penalty_, penalty.penalty_);
66 if (duration_[LEFT] < penalty.duration_[LEFT])
67 ret.push_back (Page_turn_event (duration_[LEFT], penalty.duration_[LEFT], permission_, penalty_));
69 if (penalty.permission_ != SCM_EOL)
70 ret.push_back (Page_turn_event (intersect[LEFT], intersect[RIGHT], permission_, new_pen));
72 if (penalty.duration_[RIGHT] < duration_[RIGHT])
73 ret.push_back (Page_turn_event (penalty.duration_[RIGHT], duration_[RIGHT], permission_, penalty_));
79 class Page_turn_engraver : public Engraver
84 Rational repeat_begin_rest_length_;
86 vector<Page_turn_event> forced_breaks_;
87 vector<Page_turn_event> automatic_breaks_;
88 vector<Page_turn_event> repeat_penalties_;
90 /* the next 3 are in sync (ie. same number of elements, etc.) */
91 vector<Rational> breakable_moments_;
92 vector<Grob *> breakable_columns_;
93 vector<bool> special_barlines_;
95 SCM max_permission (SCM perm1, SCM perm2);
96 Real penalty (Rational rest_len);
97 Grob *breakable_column (Page_turn_event const &);
100 DECLARE_TRANSLATOR_LISTENER (break);
101 DECLARE_ACKNOWLEDGER (note_head);
104 TRANSLATOR_DECLARATIONS (Page_turn_engraver);
105 void stop_translation_timestep ();
106 void start_translation_timestep ();
110 Page_turn_engraver::Page_turn_engraver ()
112 repeat_begin_ = Moment (-1);
113 repeat_begin_rest_length_ = 0;
119 Page_turn_engraver::breakable_column (Page_turn_event const &brk)
121 vsize start = lower_bound (breakable_moments_, brk.duration_[LEFT], less<Rational> ());
122 vsize end = upper_bound (breakable_moments_, brk.duration_[RIGHT], less<Rational> ());
124 if (start == breakable_moments_.size ())
130 for (vsize i = end + 1; i-- > start;)
131 if (special_barlines_[i])
132 return breakable_columns_[i];
134 return breakable_columns_[end];
138 Page_turn_engraver::penalty (Rational rest_len)
140 Rational min_turn = robust_scm2moment (get_property ("minimumPageTurnLength"), Moment (1)).main_part_;
142 return (rest_len < min_turn) ? infinity_f : 0;
146 Page_turn_engraver::acknowledge_note_head (Grob_info gi)
148 Stream_event *cause = gi.event_cause ();
150 Duration *dur_ptr = cause
151 ? unsmob_duration (cause->get_property ("duration"))
157 if (rest_begin_ < now_mom ())
159 Real pen = penalty ((now_mom () - rest_begin_).main_part_);
161 automatic_breaks_.push_back (Page_turn_event (rest_begin_.main_part_,
162 now_mom ().main_part_,
163 ly_symbol2scm ("allow"), 0));
166 if (rest_begin_ <= repeat_begin_)
167 repeat_begin_rest_length_ = (now_mom () - repeat_begin_).main_part_;
168 note_end_ = now_mom () + dur_ptr->get_length ();
171 IMPLEMENT_TRANSLATOR_LISTENER (Page_turn_engraver, break);
173 Page_turn_engraver::listen_break (Stream_event *ev)
175 string name = ly_symbol2string (ev->get_property ("class"));
177 if (name == "page-turn-event")
179 SCM permission = ev->get_property ("break-permission");
180 Real penalty = robust_scm2double (ev->get_property ("break-penalty"), 0);
181 Rational now = now_mom ().main_part_;
183 forced_breaks_.push_back (Page_turn_event (now, now, permission, penalty));
188 Page_turn_engraver::start_translation_timestep ()
190 /* What we want to do is to build a list of all the
191 breakable paper columns. In general, paper-columns won't be marked as
192 such until the Paper_column_engraver has done stop_translation_timestep.
194 Therefore, we just grab /all/ paper columns (in the
195 stop_translation_timestep, since they're not created here yet)
196 and remove the non-breakable ones at the beginning of the following
200 if (breakable_columns_.size () && !Paper_column::is_breakable (breakable_columns_.back ()))
202 breakable_columns_.pop_back ();
203 breakable_moments_.pop_back ();
204 special_barlines_.pop_back ();
209 Page_turn_engraver::stop_translation_timestep ()
211 Grob *pc = unsmob_grob (get_property ("currentCommandColumn"));
215 breakable_columns_.push_back (pc);
216 breakable_moments_.push_back (now_mom ().main_part_);
218 SCM bar_scm = get_property ("whichBar");
219 string bar = robust_scm2string (bar_scm, "");
221 special_barlines_.push_back (bar != "" && bar != "|");
224 /* C&P from Repeat_acknowledge_engraver */
225 SCM cs = get_property ("repeatCommands");
229 for (; scm_is_pair (cs); cs = scm_cdr (cs))
231 SCM command = scm_car (cs);
232 if (command == ly_symbol2scm ("start-repeat"))
234 else if (command == ly_symbol2scm ("end-repeat"))
238 if (end && repeat_begin_.main_part_ >= Moment (0))
240 Rational now = now_mom ().main_part_;
241 Real pen = penalty ((now_mom () - rest_begin_).main_part_ + repeat_begin_rest_length_);
242 Moment *m = unsmob_moment (get_property ("minimumRepeatLengthForPageTurn"));
243 if (m && *m > (now_mom () - repeat_begin_))
246 if (pen == infinity_f)
247 repeat_penalties_.push_back (Page_turn_event (repeat_begin_.main_part_, now, SCM_EOL, -infinity_f));
249 repeat_penalties_.push_back (Page_turn_event (repeat_begin_.main_part_, now, ly_symbol2scm ("allow"), pen));
251 repeat_begin_ = Moment (-1);
256 repeat_begin_ = now_mom ();
257 repeat_begin_rest_length_ = 0;
259 rest_begin_ = note_end_;
262 /* return the most permissive symbol (where force is the most permissive and
266 Page_turn_engraver::max_permission (SCM perm1, SCM perm2)
268 if (perm1 == SCM_EOL)
270 if (perm1 == ly_symbol2scm ("allow") && perm2 == ly_symbol2scm ("force"))
276 Page_turn_engraver::finalize ()
279 vector<Page_turn_event> auto_breaks;
281 /* filter the automatic breaks through the repeat penalties */
282 for (vsize i = 0; i < automatic_breaks_.size (); i++)
284 Page_turn_event &brk = automatic_breaks_[i];
286 /* find the next applicable repeat penalty */
288 rep_index < repeat_penalties_.size ()
289 && repeat_penalties_[rep_index].duration_[RIGHT] <= brk.duration_[LEFT];
293 if (rep_index >= repeat_penalties_.size ()
294 || brk.duration_[RIGHT] <= repeat_penalties_[rep_index].duration_[LEFT])
295 auto_breaks.push_back (brk);
298 vector<Page_turn_event> split = brk.penalize (repeat_penalties_[rep_index]);
300 /* it's possible that the last of my newly-split events overlaps the next repeat_penalty,
301 in which case we need to refilter that event */
302 if (rep_index + 1 < repeat_penalties_.size ()
304 && split.back ().duration_[RIGHT] > repeat_penalties_[rep_index + 1].duration_[LEFT])
306 automatic_breaks_[i] = split.back ();
310 auto_breaks.insert (auto_breaks.end (), split.begin (), split.end ());
314 /* apply the automatic breaks */
315 for (vsize i = 0; i < auto_breaks.size (); i++)
317 Page_turn_event const &brk = auto_breaks[i];
318 Grob *pc = breakable_column (auto_breaks[i]);
321 SCM perm = max_permission (pc->get_property ("page-turn-permission"), brk.permission_);
322 Real pen = min (robust_scm2double (pc->get_property ("page-turn-penalty"), infinity_f), brk.penalty_);
323 pc->set_property ("page-turn-permission", perm);
324 pc->set_property ("page-turn-penalty", scm_from_double (pen));
328 /* unless a manual break overrides it, allow a page turn at the end of the piece */
329 breakable_columns_.back ()->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
331 /* apply the manual breaks */
332 for (vsize i = 0; i < forced_breaks_.size (); i++)
334 Page_turn_event const &brk = forced_breaks_[i];
335 Grob *pc = breakable_column (forced_breaks_[i]);
338 pc->set_property ("page-turn-permission", brk.permission_);
339 pc->set_property ("page-turn-penalty", scm_from_double (brk.penalty_));
344 ADD_ACKNOWLEDGER (Page_turn_engraver, note_head);
346 ADD_TRANSLATOR (Page_turn_engraver,
348 "Decide where page turns are allowed to go.",
354 "minimumPageTurnLength "
355 "minimumRepeatLengthForPageTurn ",