]> git.donarmstrong.com Git - lilypond.git/blob - lily/multi-measure-rest-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[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 using std::vector;
33
34 /**
35    The name says it all: make multi measure rests
36 */
37 class Multi_measure_rest_engraver : public Engraver
38 {
39 public:
40   TRANSLATOR_DECLARATIONS (Multi_measure_rest_engraver);
41
42 protected:
43   void process_music ();
44   void start_translation_timestep ();
45   DECLARE_TRANSLATOR_LISTENER (multi_measure_rest);
46   DECLARE_TRANSLATOR_LISTENER (multi_measure_text);
47
48 private:
49   void add_bound_item_to_grobs (Item *);
50   void clear_lapsed_events (const Moment &now);
51   bool grobs_initialized () const { return mmrest_; }
52   void initialize_grobs ();
53   void reset_grobs ();
54   void set_measure_count (int n);
55
56 private:
57   vector<Stream_event *> text_events_;
58   // text_[0] is a MultiMeasureRestNumber grob
59   // the rest are optional MultiMeasureRestText grobs
60   vector<Spanner *> text_;
61   Stream_event *rest_ev_;
62   Spanner *mmrest_;
63   Moment stop_moment_;
64   int start_measure_;
65   // Ugh, this is a kludge - need this for multi-measure-rest-grace.ly
66   Item *last_command_item_;
67   bool first_time_;
68 };
69
70 Multi_measure_rest_engraver::Multi_measure_rest_engraver ()
71   : rest_ev_ (0),
72     mmrest_ (0),
73     start_measure_ (0),
74     last_command_item_ (0),
75     first_time_ (true)
76 {
77 }
78
79 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_rest);
80 void
81 Multi_measure_rest_engraver::listen_multi_measure_rest (Stream_event *ev)
82 {
83   /* FIXME: Should use ASSIGN_EVENT_ONCE. Can't do that yet because of
84      the kill-mm-rests hack in part-combine-iterator. */
85   rest_ev_ = ev;
86   const Moment now (now_mom ());
87   stop_moment_ = now + get_event_length (rest_ev_, now);
88   /*
89   if (ASSIGN_EVENT_ONCE (rest_ev_, ev))
90     stop_moment_ = now_mom () + get_event_length (rest_ev_);
91   */
92
93   clear_lapsed_events (now);
94 }
95
96 IMPLEMENT_TRANSLATOR_LISTENER (Multi_measure_rest_engraver, multi_measure_text);
97 void
98 Multi_measure_rest_engraver::listen_multi_measure_text (Stream_event *ev)
99 {
100   text_events_.push_back (ev);
101 }
102
103 void
104 Multi_measure_rest_engraver::add_bound_item_to_grobs (Item *item)
105 {
106   add_bound_item (mmrest_, item);
107   for (vsize i = 0; i < text_.size (); ++i)
108     add_bound_item (text_[i], item);
109 }
110
111 void
112 Multi_measure_rest_engraver::clear_lapsed_events (const Moment &now)
113 {
114   if (now.main_part_ >= stop_moment_.main_part_)
115     {
116       rest_ev_ = 0;
117       text_events_.clear ();
118     }
119 }
120
121 void
122 Multi_measure_rest_engraver::initialize_grobs ()
123 {
124   mmrest_ = make_spanner ("MultiMeasureRest", rest_ev_->self_scm ());
125   text_.push_back (make_spanner ("MultiMeasureRestNumber",
126                                  rest_ev_->self_scm ()));
127
128   if (text_events_.size ())
129     {
130       for (vsize i = 0; i < text_events_.size (); i++)
131         {
132           Stream_event *e = text_events_[i];
133           Spanner *sp = make_spanner ("MultiMeasureRestText", e->self_scm ());
134           SCM t = e->get_property ("text");
135           SCM dir = e->get_property ("direction");
136           sp->set_property ("text", t);
137           if (is_direction (dir))
138             sp->set_property ("direction", dir);
139
140           text_.push_back (sp);
141         }
142
143       /*
144         Stack different scripts.
145       */
146       for (DOWN_and_UP (d))
147         {
148           SCM dir = scm_from_int (d);
149           Grob *last = 0;
150           for (vsize i = 0; i < text_.size (); i++)
151             {
152               if (scm_is_eq (dir, text_[i]->get_property ("direction")))
153                 {
154                   if (last)
155                     Side_position_interface::add_support (text_[i], last);
156                   last = text_[i];
157                 }
158             }
159         }
160     }
161
162   for (vsize i = 0; i < text_.size (); i++)
163     {
164       Side_position_interface::add_support (text_[i], mmrest_);
165       text_[i]->set_parent (mmrest_, Y_AXIS);
166       text_[i]->set_parent (mmrest_, X_AXIS);
167     }
168 }
169
170 void
171 Multi_measure_rest_engraver::reset_grobs ()
172 {
173   text_.clear ();
174   mmrest_ = 0;
175 }
176
177 void
178 Multi_measure_rest_engraver::set_measure_count (int n)
179 {
180   SCM n_scm = scm_from_int (n);
181   assert (mmrest_);
182   mmrest_->set_property ("measure-count", n_scm);
183
184   Grob *g = text_[0]; // the MultiMeasureRestNumber
185   assert (g);
186   if (scm_is_null (g->get_property ("text")))
187     {
188       SCM thres = get_property ("restNumberThreshold");
189       int t = 1;
190       if (scm_is_number (thres))
191         t = scm_to_int (thres);
192
193       if (n <= t)
194         g->suicide ();
195       else
196         {
197           SCM text = scm_number_to_string (n_scm, scm_from_int (10));
198           g->set_property ("text", text);
199         }
200     }
201 }
202
203 void
204 Multi_measure_rest_engraver::process_music ()
205 {
206   const bool measure_end
207   = scm_is_string (get_property ("whichBar"))
208     && (robust_scm2moment (get_property ("measurePosition"),
209                            Moment (0)).main_part_ == Rational (0));
210
211   if (measure_end || first_time_)
212     {
213       last_command_item_ = unsmob<Item> (get_property ("currentCommandColumn"));
214
215       // Finalize the current grobs.
216       if (grobs_initialized ())
217         {
218           int curr_measure = scm_to_int (get_property ("internalBarNumber"));
219           set_measure_count (curr_measure - start_measure_);
220           if (last_command_item_)
221             add_bound_item_to_grobs (last_command_item_);
222           reset_grobs ();
223         }
224     }
225
226   // Create new grobs if a rest event is (still) active.
227   if (!grobs_initialized () && rest_ev_)
228     {
229       initialize_grobs ();
230       text_events_.clear ();
231
232       if (last_command_item_)
233         {
234           add_bound_item_to_grobs (last_command_item_);
235           last_command_item_ = 0;
236         }
237
238       start_measure_ = scm_to_int (get_property ("internalBarNumber"));
239     }
240
241   first_time_ = false;
242 }
243
244 void
245 Multi_measure_rest_engraver::start_translation_timestep ()
246 {
247   clear_lapsed_events (now_mom ());
248 }
249
250 ADD_TRANSLATOR (Multi_measure_rest_engraver,
251                 /* doc */
252                 "Engrave multi-measure rests that are produced with"
253                 " @samp{R}.  It reads @code{measurePosition} and"
254                 " @code{internalBarNumber} to determine what number to print"
255                 " over the @ref{MultiMeasureRest}.",
256
257                 /* create */
258                 "MultiMeasureRest "
259                 "MultiMeasureRestNumber "
260                 "MultiMeasureRestText ",
261
262                 /* read */
263                 "internalBarNumber "
264                 "restNumberThreshold "
265                 "currentCommandColumn "
266                 "measurePosition "
267                 "whichBar ",
268
269                 /* write */
270                 ""
271                );