Update from Francisco.
[lilypond.git] / lily / multi-measure-rest-engraver.cc
blobfc5563854cc2295fc5407cd33c0aceb40790ee27
1 /*
2 multi_measure_rest-engraver.cc -- implement Multi_measure_rest_engraver
4 (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 */
8 #include "multi-measure-rest.hh"
9 #include "paper-column.hh"
10 #include "engraver-group.hh"
11 #include "side-position-interface.hh"
12 #include "staff-symbol-referencer.hh"
13 #include "stream-event.hh"
14 #include "moment.hh"
15 #include "spanner.hh"
17 #include "translator.icc"
19 /**
20 The name says it all: make multi measure rests
22 class Multi_measure_rest_engraver : public Engraver
24 public:
25 TRANSLATOR_DECLARATIONS (Multi_measure_rest_engraver);
27 protected:
28 void process_music ();
29 void stop_translation_timestep ();
30 void start_translation_timestep ();
31 virtual void finalize ();
32 DECLARE_TRANSLATOR_LISTENER (multi_measure_rest);
33 DECLARE_TRANSLATOR_LISTENER (multi_measure_text);
35 private:
36 Stream_event *rest_ev_;
37 vector<Stream_event*> text_events_;
38 int start_measure_;
39 Rational last_main_moment_;
40 Moment stop_moment_;
42 bool bar_seen_;
43 Item *last_command_item_;
44 Spanner *last_rest_;
45 Spanner *mmrest_;
47 vector<Spanner*> numbers_;
48 vector<Spanner*> last_numbers_;
51 Multi_measure_rest_engraver::Multi_measure_rest_engraver ()
53 last_command_item_ = 0;
56 For the start of a score.
58 bar_seen_ = true;
59 start_measure_ = 0;
60 mmrest_ = 0;
61 last_rest_ = 0;
62 rest_ev_ = 0;
65 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_rest);
66 void
67 Multi_measure_rest_engraver::listen_multi_measure_rest (Stream_event *ev)
69 /* FIXME: Should use ASSIGN_EVENT_ONCE. Can't do that yet because of
70 the kill-mm-rests hack in part-combine-iterator. */
71 rest_ev_ = ev;
72 stop_moment_ = now_mom () + get_event_length (rest_ev_, now_mom ());
74 if (ASSIGN_EVENT_ONCE (rest_ev_, ev))
75 stop_moment_ = now_mom () + get_event_length (rest_ev_);
79 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_text);
80 void
81 Multi_measure_rest_engraver::listen_multi_measure_text (Stream_event *ev)
83 text_events_.push_back (ev);
86 void
87 Multi_measure_rest_engraver::process_music ()
89 if (rest_ev_ && !mmrest_
90 && stop_moment_ > now_mom ())
92 mmrest_ = make_spanner ("MultiMeasureRest", rest_ev_->self_scm ());
94 Spanner *sp
95 = make_spanner ("MultiMeasureRestNumber", rest_ev_->self_scm ());
96 numbers_.push_back (sp);
98 if (text_events_.size ())
100 for (vsize i = 0; i < text_events_.size (); i++)
102 Stream_event *e = text_events_[i];
103 Spanner *sp
104 = make_spanner ("MultiMeasureRestText", e->self_scm ());
105 SCM t = e->get_property ("text");
106 SCM dir = e->get_property ("direction");
107 sp->set_property ("text", t);
108 if (is_direction (dir))
109 sp->set_property ("direction", dir);
111 numbers_.push_back (sp);
115 Stack different scripts.
117 Direction d = DOWN;
120 Grob *last = 0;
121 for (vsize i = 0; i < numbers_.size (); i++)
123 if (scm_from_int (d) == numbers_[i]->get_property ("direction"))
125 if (last)
126 Side_position_interface::add_support (numbers_[i], last);
127 last = numbers_[i];
131 while (flip (&d) != DOWN);
134 for (vsize i = 0; i < numbers_.size (); i++)
136 Side_position_interface::add_support (numbers_[i], mmrest_);
137 numbers_[i]->set_parent (mmrest_, Y_AXIS);
140 start_measure_
141 = scm_to_int (get_property ("internalBarNumber"));
144 bar_seen_ = bar_seen_ || scm_is_string (get_property ("whichBar"));
147 void
148 Multi_measure_rest_engraver::stop_translation_timestep ()
150 /* We cannot do this earlier, as breakableSeparationItem is not yet
151 there.
153 Actually, we no longer use breakableSeparationItem -- should this be moved?
154 -- jneem */
155 if (bar_seen_)
157 Grob *cmc = unsmob_grob (get_property ("currentCommandColumn"));
159 /* Ugh, this is a kludge - need this for multi-measure-rest-grace.ly */
160 last_command_item_ = dynamic_cast<Item *> (cmc);
163 if (last_command_item_ && (mmrest_ || last_rest_))
165 if (last_rest_)
167 add_bound_item (last_rest_, last_command_item_);
168 for (vsize i = 0; i < last_numbers_.size (); i++)
169 add_bound_item (last_numbers_[i], last_command_item_);
172 if (mmrest_)
174 add_bound_item (mmrest_, last_command_item_);
175 for (vsize i = 0; i < numbers_.size (); i++)
176 add_bound_item (numbers_[i], last_command_item_);
178 last_command_item_ = 0;
182 Moment mp (robust_scm2moment (get_property ("measurePosition"), Moment (0)));
183 if (last_rest_)
185 last_rest_ = 0;
186 last_numbers_.clear ();
189 text_events_.clear ();
190 bar_seen_ = false;
193 void
194 Multi_measure_rest_engraver::start_translation_timestep ()
196 if (now_mom ().main_part_ >= stop_moment_.main_part_)
197 rest_ev_ = 0;
199 Moment mp (robust_scm2moment (get_property ("measurePosition"), Moment (0)));
201 Moment now = now_mom ();
202 if (mmrest_
203 && now.main_part_ != last_main_moment_
204 && mp.main_part_ == Rational (0))
206 last_rest_ = mmrest_;
207 last_numbers_ = numbers_;
209 int cur = scm_to_int (get_property ("internalBarNumber"));
210 int num = cur - start_measure_;
213 We can't plug a markup directly into the grob, since the
214 measure-count determines the formatting of the mmrest.
216 last_rest_->set_property ("measure-count", scm_from_int (num));
218 SCM sml = get_property ("measureLength");
219 Rational ml = (unsmob_moment (sml)) ? unsmob_moment (sml)->main_part_ : Rational (1);
220 if (ml >= Rational (2))
221 last_rest_->set_property ("use-breve-rest", SCM_BOOL_T);
223 mmrest_ = 0;
224 numbers_.clear ();
226 Grob *last = last_numbers_.size () ? last_numbers_[0] : 0;
227 if (last && last->get_property ("text") == SCM_EOL)
229 SCM thres = get_property ("restNumberThreshold");
230 int t = 1;
231 if (scm_is_number (thres))
232 t = scm_to_int (thres);
234 if (num <= t)
235 last->suicide ();
236 else
238 SCM text
239 = scm_number_to_string (scm_from_int (num), scm_from_int (10));
240 last->set_property ("text", text);
245 last_main_moment_ = now.main_part_;
248 void
249 Multi_measure_rest_engraver::finalize ()
253 ADD_TRANSLATOR (Multi_measure_rest_engraver,
254 /* doc */
255 "Engrave multi-measure rests that are produced with"
256 " @samp{R}. It reads @code{measurePosition} and"
257 " @code{internalBarNumber} to determine what number to print"
258 " over the @ref{MultiMeasureRest}. Reads @code{measureLength}"
259 " to determine whether it should use a whole rest or a breve"
260 " rest to represent one measure.",
262 /* create */
263 "MultiMeasureRest "
264 "MultiMeasureRestNumber "
265 "MultiMeasureRestText ",
267 /* read */
268 "internalBarNumber "
269 "restNumberThreshold "
270 "breakableSeparationItem "
271 "currentCommandColumn "
272 "measurePosition "
273 "measureLength ",
275 /* write */