2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "multi-measure-rest.hh"
22 #include "paper-column.hh"
23 #include "engraver-group.hh"
24 #include "side-position-interface.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "stream-event.hh"
30 #include "translator.icc"
33 The name says it all: make multi measure rests
35 class Multi_measure_rest_engraver : public Engraver
38 TRANSLATOR_DECLARATIONS (Multi_measure_rest_engraver);
41 void process_music ();
42 void start_translation_timestep ();
43 DECLARE_TRANSLATOR_LISTENER (multi_measure_rest);
44 DECLARE_TRANSLATOR_LISTENER (multi_measure_text);
47 void add_bound_item_to_grobs (Item *);
48 void clear_lapsed_events (const Moment &now);
49 bool grobs_initialized () const { return mmrest_; }
50 void initialize_grobs ();
52 void set_measure_count (int n);
55 vector<Stream_event *> text_events_;
56 // text_[0] is a MultiMeasureRestNumber grob
57 // the rest are optional MultiMeasureRestText grobs
58 vector<Spanner *> text_;
59 Stream_event *rest_ev_;
63 // Ugh, this is a kludge - need this for multi-measure-rest-grace.ly
64 Item *last_command_item_;
68 Multi_measure_rest_engraver::Multi_measure_rest_engraver ()
72 last_command_item_ (0),
77 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_rest);
79 Multi_measure_rest_engraver::listen_multi_measure_rest (Stream_event *ev)
81 /* FIXME: Should use ASSIGN_EVENT_ONCE. Can't do that yet because of
82 the kill-mm-rests hack in part-combine-iterator. */
84 const Moment now (now_mom ());
85 stop_moment_ = now + get_event_length (rest_ev_, now);
87 if (ASSIGN_EVENT_ONCE (rest_ev_, ev))
88 stop_moment_ = now_mom () + get_event_length (rest_ev_);
91 clear_lapsed_events (now);
94 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_text);
96 Multi_measure_rest_engraver::listen_multi_measure_text (Stream_event *ev)
98 text_events_.push_back (ev);
102 Multi_measure_rest_engraver::add_bound_item_to_grobs (Item *item)
104 add_bound_item (mmrest_, item);
105 for (vsize i = 0; i < text_.size (); ++i)
106 add_bound_item (text_[i], item);
110 Multi_measure_rest_engraver::clear_lapsed_events (const Moment &now)
112 if (now.main_part_ >= stop_moment_.main_part_)
115 text_events_.clear ();
120 Multi_measure_rest_engraver::initialize_grobs ()
122 mmrest_ = make_spanner ("MultiMeasureRest", rest_ev_->self_scm ());
123 text_.push_back (make_spanner ("MultiMeasureRestNumber",
124 rest_ev_->self_scm ()));
126 if (text_events_.size ())
128 for (vsize i = 0; i < text_events_.size (); i++)
130 Stream_event *e = text_events_[i];
131 Spanner *sp = make_spanner ("MultiMeasureRestText", e->self_scm ());
132 SCM t = e->get_property ("text");
133 SCM dir = e->get_property ("direction");
134 sp->set_property ("text", t);
135 if (is_direction (dir))
136 sp->set_property ("direction", dir);
138 text_.push_back (sp);
142 Stack different scripts.
144 for (DOWN_and_UP (d))
146 SCM dir = scm_from_int (d);
148 for (vsize i = 0; i < text_.size (); i++)
150 if (scm_is_eq (dir, text_[i]->get_property ("direction")))
153 Side_position_interface::add_support (text_[i], last);
160 for (vsize i = 0; i < text_.size (); i++)
162 Side_position_interface::add_support (text_[i], mmrest_);
163 text_[i]->set_parent (mmrest_, Y_AXIS);
164 text_[i]->set_parent (mmrest_, X_AXIS);
169 Multi_measure_rest_engraver::reset_grobs ()
176 Multi_measure_rest_engraver::set_measure_count (int n)
178 SCM n_scm = scm_from_int (n);
180 mmrest_->set_property ("measure-count", n_scm);
182 Grob *g = text_[0]; // the MultiMeasureRestNumber
184 if (scm_is_null (g->get_property ("text")))
186 SCM thres = get_property ("restNumberThreshold");
188 if (scm_is_number (thres))
189 t = scm_to_int (thres);
195 SCM text = scm_number_to_string (n_scm, scm_from_int (10));
196 g->set_property ("text", text);
202 Multi_measure_rest_engraver::process_music ()
204 const bool measure_end
205 = scm_is_string (get_property ("whichBar"))
206 && (robust_scm2moment (get_property ("measurePosition"),
207 Moment (0)).main_part_ == Rational (0));
209 if (measure_end || first_time_)
211 last_command_item_ = unsmob<Item> (get_property ("currentCommandColumn"));
213 // Finalize the current grobs.
214 if (grobs_initialized ())
216 int curr_measure = scm_to_int (get_property ("internalBarNumber"));
217 set_measure_count (curr_measure - start_measure_);
218 if (last_command_item_)
219 add_bound_item_to_grobs (last_command_item_);
224 // Create new grobs if a rest event is (still) active.
225 if (!grobs_initialized () && rest_ev_)
228 text_events_.clear ();
230 if (last_command_item_)
232 add_bound_item_to_grobs (last_command_item_);
233 last_command_item_ = 0;
236 start_measure_ = scm_to_int (get_property ("internalBarNumber"));
243 Multi_measure_rest_engraver::start_translation_timestep ()
245 clear_lapsed_events (now_mom ());
248 ADD_TRANSLATOR (Multi_measure_rest_engraver,
250 "Engrave multi-measure rests that are produced with"
251 " @samp{R}. It reads @code{measurePosition} and"
252 " @code{internalBarNumber} to determine what number to print"
253 " over the @ref{MultiMeasureRest}.",
257 "MultiMeasureRestNumber "
258 "MultiMeasureRestText ",
262 "restNumberThreshold "
263 "currentCommandColumn "