]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest-engraver.cc
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[lilypond.git] / lily / multi-measure-rest-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
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.
11
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.
16
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/>.
19 */
20
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"
27 #include "moment.hh"
28 #include "spanner.hh"
29
30 #include "translator.icc"
31
32 /**
33    The name says it all: make multi measure rests
34 */
35 class Multi_measure_rest_engraver : public Engraver
36 {
37 public:
38   TRANSLATOR_DECLARATIONS (Multi_measure_rest_engraver);
39
40 protected:
41   void process_music ();
42   void start_translation_timestep ();
43   DECLARE_TRANSLATOR_LISTENER (multi_measure_rest);
44   DECLARE_TRANSLATOR_LISTENER (multi_measure_text);
45
46 private:
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 ();
51   void reset_grobs ();
52   void set_measure_count (int n);
53
54 private:
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_;
60   Spanner *mmrest_;
61   Moment stop_moment_;
62   int start_measure_;
63   // Ugh, this is a kludge - need this for multi-measure-rest-grace.ly
64   Item *last_command_item_;
65   bool first_time_;
66 };
67
68 Multi_measure_rest_engraver::Multi_measure_rest_engraver ()
69   : rest_ev_ (0),
70     mmrest_ (0),
71     start_measure_ (0),
72     last_command_item_ (0),
73     first_time_ (true)
74 {
75 }
76
77 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_rest);
78 void
79 Multi_measure_rest_engraver::listen_multi_measure_rest (Stream_event *ev)
80 {
81   /* FIXME: Should use ASSIGN_EVENT_ONCE. Can't do that yet because of
82      the kill-mm-rests hack in part-combine-iterator. */
83   rest_ev_ = ev;
84   const Moment now (now_mom ());
85   stop_moment_ = now + get_event_length (rest_ev_, now);
86   /*
87   if (ASSIGN_EVENT_ONCE (rest_ev_, ev))
88     stop_moment_ = now_mom () + get_event_length (rest_ev_);
89   */
90
91   clear_lapsed_events (now);
92 }
93
94 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_text);
95 void
96 Multi_measure_rest_engraver::listen_multi_measure_text (Stream_event *ev)
97 {
98   text_events_.push_back (ev);
99 }
100
101 void
102 Multi_measure_rest_engraver::add_bound_item_to_grobs (Item *item)
103 {
104   add_bound_item (mmrest_, item);
105   for (vsize i = 0; i < text_.size (); ++i)
106     add_bound_item (text_[i], item);
107 }
108
109 void
110 Multi_measure_rest_engraver::clear_lapsed_events (const Moment &now)
111 {
112   if (now.main_part_ >= stop_moment_.main_part_)
113     {
114       rest_ev_ = 0;
115       text_events_.clear ();
116     }
117 }
118
119 void
120 Multi_measure_rest_engraver::initialize_grobs ()
121 {
122   mmrest_ = make_spanner ("MultiMeasureRest", rest_ev_->self_scm ());
123   text_.push_back (make_spanner ("MultiMeasureRestNumber",
124                                  rest_ev_->self_scm ()));
125
126   if (text_events_.size ())
127     {
128       for (vsize i = 0; i < text_events_.size (); i++)
129         {
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);
137
138           text_.push_back (sp);
139         }
140
141       /*
142         Stack different scripts.
143       */
144       for (DOWN_and_UP (d))
145         {
146           SCM dir = scm_from_int (d);
147           Grob *last = 0;
148           for (vsize i = 0; i < text_.size (); i++)
149             {
150               if (scm_is_eq (dir, text_[i]->get_property ("direction")))
151                 {
152                   if (last)
153                     Side_position_interface::add_support (text_[i], last);
154                   last = text_[i];
155                 }
156             }
157         }
158     }
159
160   for (vsize i = 0; i < text_.size (); i++)
161     {
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);
165     }
166 }
167
168 void
169 Multi_measure_rest_engraver::reset_grobs ()
170 {
171   text_.clear ();
172   mmrest_ = 0;
173 }
174
175 void
176 Multi_measure_rest_engraver::set_measure_count (int n)
177 {
178   SCM n_scm = scm_from_int (n);
179   assert (mmrest_);
180   mmrest_->set_property ("measure-count", n_scm);
181
182   Grob *g = text_[0]; // the MultiMeasureRestNumber
183   assert (g);
184   if (scm_is_null (g->get_property ("text")))
185     {
186       SCM thres = get_property ("restNumberThreshold");
187       int t = 1;
188       if (scm_is_number (thres))
189         t = scm_to_int (thres);
190
191       if (n <= t)
192         g->suicide ();
193       else
194         {
195           SCM text = scm_number_to_string (n_scm, scm_from_int (10));
196           g->set_property ("text", text);
197         }
198     }
199 }
200
201 void
202 Multi_measure_rest_engraver::process_music ()
203 {
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));
208
209   if (measure_end || first_time_)
210     {
211       last_command_item_ = unsmob<Item> (get_property ("currentCommandColumn"));
212
213       // Finalize the current grobs.
214       if (grobs_initialized ())
215         {
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_);
220           reset_grobs ();
221         }
222     }
223
224   // Create new grobs if a rest event is (still) active.
225   if (!grobs_initialized () && rest_ev_)
226     {
227       initialize_grobs ();
228       text_events_.clear ();
229
230       if (last_command_item_)
231         {
232           add_bound_item_to_grobs (last_command_item_);
233           last_command_item_ = 0;
234         }
235
236       start_measure_ = scm_to_int (get_property ("internalBarNumber"));
237     }
238
239   first_time_ = false;
240 }
241
242 void
243 Multi_measure_rest_engraver::start_translation_timestep ()
244 {
245   clear_lapsed_events (now_mom ());
246 }
247
248 ADD_TRANSLATOR (Multi_measure_rest_engraver,
249                 /* doc */
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}.",
254
255                 /* create */
256                 "MultiMeasureRest "
257                 "MultiMeasureRestNumber "
258                 "MultiMeasureRestText ",
259
260                 /* read */
261                 "internalBarNumber "
262                 "restNumberThreshold "
263                 "currentCommandColumn "
264                 "measurePosition "
265                 "whichBar ",
266
267                 /* write */
268                 ""
269                );