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 void listen_multi_measure_rest (Stream_event *);
44 void listen_multi_measure_text (Stream_event *);
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 (Context *c)
73 last_command_item_ (0),
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);
95 Multi_measure_rest_engraver::listen_multi_measure_text (Stream_event *ev)
97 text_events_.push_back (ev);
101 Multi_measure_rest_engraver::add_bound_item_to_grobs (Item *item)
103 add_bound_item (mmrest_, item);
104 for (vsize i = 0; i < text_.size (); ++i)
105 add_bound_item (text_[i], item);
109 Multi_measure_rest_engraver::clear_lapsed_events (const Moment &now)
111 if (now.main_part_ >= stop_moment_.main_part_)
114 text_events_.clear ();
119 Multi_measure_rest_engraver::initialize_grobs ()
121 mmrest_ = make_spanner ("MultiMeasureRest", rest_ev_->self_scm ());
122 text_.push_back (make_spanner ("MultiMeasureRestNumber",
123 mmrest_->self_scm ()));
125 if (text_events_.size ())
127 for (vsize i = 0; i < text_events_.size (); i++)
129 Stream_event *e = text_events_[i];
130 Spanner *sp = make_spanner ("MultiMeasureRestText", e->self_scm ());
131 SCM t = e->get_property ("text");
132 SCM dir = e->get_property ("direction");
133 sp->set_property ("text", t);
134 if (is_direction (dir))
135 sp->set_property ("direction", dir);
137 text_.push_back (sp);
141 Stack different scripts.
143 for (DOWN_and_UP (d))
145 SCM dir = scm_from_int (d);
147 for (vsize i = 0; i < text_.size (); i++)
149 if (ly_is_equal (dir, text_[i]->get_property ("direction")))
152 Side_position_interface::add_support (text_[i], last);
159 for (vsize i = 0; i < text_.size (); i++)
161 Side_position_interface::add_support (text_[i], mmrest_);
162 text_[i]->set_parent (mmrest_, Y_AXIS);
163 text_[i]->set_parent (mmrest_, X_AXIS);
168 Multi_measure_rest_engraver::reset_grobs ()
175 Multi_measure_rest_engraver::set_measure_count (int n)
177 SCM n_scm = scm_from_int (n);
179 mmrest_->set_property ("measure-count", n_scm);
181 Grob *g = text_[0]; // the MultiMeasureRestNumber
183 if (scm_is_null (g->get_property ("text")))
185 SCM thres = get_property ("restNumberThreshold");
187 if (scm_is_number (thres))
188 t = scm_to_int (thres);
194 SCM text = scm_number_to_string (n_scm, scm_from_int (10));
195 g->set_property ("text", text);
201 Multi_measure_rest_engraver::process_music ()
203 const bool measure_end
204 = scm_is_string (get_property ("whichBar"))
205 && (robust_scm2moment (get_property ("measurePosition"),
206 Moment (0)).main_part_ == Rational (0));
208 if (measure_end || first_time_)
210 last_command_item_ = unsmob<Item> (get_property ("currentCommandColumn"));
212 // Finalize the current grobs.
213 if (grobs_initialized ())
215 int curr_measure = scm_to_int (get_property ("internalBarNumber"));
216 set_measure_count (curr_measure - start_measure_);
217 if (last_command_item_)
218 add_bound_item_to_grobs (last_command_item_);
223 // Create new grobs if a rest event is (still) active.
224 if (!grobs_initialized () && rest_ev_)
227 text_events_.clear ();
229 if (last_command_item_)
231 add_bound_item_to_grobs (last_command_item_);
232 last_command_item_ = 0;
235 start_measure_ = scm_to_int (get_property ("internalBarNumber"));
242 Multi_measure_rest_engraver::start_translation_timestep ()
244 clear_lapsed_events (now_mom ());
248 Multi_measure_rest_engraver::boot ()
250 ADD_LISTENER (Multi_measure_rest_engraver, multi_measure_rest);
251 ADD_LISTENER (Multi_measure_rest_engraver, multi_measure_text);
254 ADD_TRANSLATOR (Multi_measure_rest_engraver,
256 "Engrave multi-measure rests that are produced with"
257 " @samp{R}. It reads @code{measurePosition} and"
258 " @code{internalBarNumber} to determine what number to print"
259 " over the @ref{MultiMeasureRest}.",
263 "MultiMeasureRestNumber "
264 "MultiMeasureRestText ",
268 "restNumberThreshold "
269 "currentCommandColumn "