]> git.donarmstrong.com Git - lilypond.git/blob - lily/percent-repeat-engraver.cc
398fbe9aa0f19f7652792b8d6ff392b40cfca75a
[lilypond.git] / lily / percent-repeat-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>, Erik Sandberg <mandolaerik@gmail.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "score-engraver.hh"
22
23 #include "bar-line.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "item.hh"
27 #include "misc.hh"
28 #include "repeated-music.hh"
29 #include "side-position-interface.hh"
30 #include "spanner.hh"
31 #include "stream-event.hh"
32 #include "warn.hh"
33
34 #include "translator.icc"
35
36 /*
37 * TODO: Create separate Double_percent_repeat_engraver? 
38 * Or, at least move double percent handling to Slash_repeat_engraver
39 */
40
41 class Percent_repeat_engraver : public Engraver
42 {
43   void typeset_perc ();
44   bool check_count_visibility (SCM count);
45 public:
46   TRANSLATOR_DECLARATIONS (Percent_repeat_engraver);
47   
48 protected:
49   Stream_event *percent_event_;
50
51   /// moment (global time) where percent started.
52   Moment stop_mom_;
53   Moment start_mom_;
54
55   enum Repeat_sign_type
56     {
57       UNKNOWN,
58       MEASURE,
59       DOUBLE_MEASURE,
60     };
61   Repeat_sign_type repeat_sign_type_;
62
63   Spanner *percent_;
64   Spanner *percent_counter_;
65
66   
67   Grob *first_command_column_;
68   Moment command_moment_;
69   
70 protected:
71   virtual void finalize ();
72   DECLARE_TRANSLATOR_LISTENER (percent);
73
74   void stop_translation_timestep ();
75   void start_translation_timestep ();
76   void process_music ();
77 };
78
79 Percent_repeat_engraver::Percent_repeat_engraver ()
80 {
81   percent_ = 0;
82   percent_counter_ = 0;
83   percent_event_ = 0;
84
85   first_command_column_ = 0;
86   command_moment_ = Moment (-1);
87 }
88
89 void
90 Percent_repeat_engraver::start_translation_timestep ()
91 {
92   if (now_mom ().main_part_ != command_moment_.main_part_)
93     {
94       first_command_column_ = unsmob_grob (get_property ("currentCommandColumn"));
95       command_moment_ = now_mom ();
96     }
97
98   if (stop_mom_.main_part_ == now_mom ().main_part_)
99     {
100       if (percent_)
101         typeset_perc ();
102       percent_event_ = 0;
103       repeat_sign_type_ = UNKNOWN;
104     }
105 }
106
107 IMPLEMENT_TRANSLATOR_LISTENER (Percent_repeat_engraver, percent);
108 void
109 Percent_repeat_engraver::listen_percent (Stream_event *ev)
110 {
111   if (!percent_event_)
112     {
113       Moment body_length = get_event_length (ev);
114       Moment meas_len (robust_scm2moment (get_property ("measureLength"),
115                                           Moment (1)));
116       if (meas_len == body_length)
117         {
118           repeat_sign_type_ = MEASURE;
119           start_mom_ = now_mom ();
120           stop_mom_ = now_mom () + body_length;
121           get_global_context ()->add_moment_to_process (stop_mom_);
122         }
123       else if (Moment (2) * meas_len == body_length)
124         {
125           repeat_sign_type_ = DOUBLE_MEASURE;
126           start_mom_ = now_mom () + meas_len;
127           stop_mom_ = now_mom () + body_length; /* never used */
128           get_global_context ()->add_moment_to_process (start_mom_);
129         }
130       else
131         {
132           /*
133             don't warn about percent repeats: slash repeats are not
134             exactly 1 or 2 measures long.
135           */
136           return;
137         }
138       percent_event_ = ev;
139     }
140   else
141     /* print a warning: no assignment happens because
142        percent_event_ != 0 */
143     ASSIGN_EVENT_ONCE (percent_event_, ev);
144 }
145
146 void
147 Percent_repeat_engraver::process_music ()
148 {
149   if (percent_event_ && now_mom ().main_part_ == start_mom_.main_part_)
150     {
151       if (repeat_sign_type_ == MEASURE)
152         {
153           if (percent_)
154             typeset_perc ();
155           
156           percent_ = make_spanner ("PercentRepeat", percent_event_->self_scm ());
157
158           Grob *col = first_command_column_;
159           percent_->set_bound (LEFT, col);
160
161           SCM count = percent_event_->get_property ("repeat-count");
162           if (count != SCM_EOL && to_boolean (get_property ("countPercentRepeats"))
163               && check_count_visibility (count))
164             {
165               percent_counter_
166                 = make_spanner ("PercentRepeatCounter", percent_event_->self_scm ());
167
168               SCM text = scm_number_to_string (count, scm_from_int (10));
169               percent_counter_->set_property ("text", text);
170               percent_counter_->set_bound (LEFT, col);
171               Side_position_interface::add_support (percent_counter_,
172                                                     percent_);
173               percent_counter_->set_parent (percent_, Y_AXIS);
174             }
175           else
176             percent_counter_ = 0;
177         }
178       else if (repeat_sign_type_ == DOUBLE_MEASURE)
179         {
180           Item *double_percent = make_item ("DoublePercentRepeat", percent_event_->self_scm ());
181
182           SCM count = percent_event_->get_property ("repeat-count");
183           if (count != SCM_EOL && to_boolean (get_property ("countPercentRepeats"))
184               && check_count_visibility (count))
185             {
186               Item *double_percent_counter = make_item ("DoublePercentRepeatCounter",
187                                                         percent_event_->self_scm ());
188
189               SCM text = scm_number_to_string (count,
190                                                scm_from_int (10));
191               double_percent_counter->set_property ("text", text);
192
193               Side_position_interface::add_support (double_percent_counter,
194                                                     double_percent);
195               double_percent_counter->set_parent (double_percent, Y_AXIS);
196               double_percent_counter->set_parent (double_percent, X_AXIS);
197             }
198           
199           /* forbid breaks on a % line. Should forbid all breaks, really. */
200           context ()->get_score_context ()->set_property ("forbidBreak", SCM_BOOL_T);
201
202           /* No more processing needed. */
203           repeat_sign_type_ = UNKNOWN;
204         }
205     }
206 }
207
208 void
209 Percent_repeat_engraver::finalize ()
210 {
211   if (percent_)
212     {
213       percent_event_->origin ()->warning (_ ("unterminated percent repeat"));
214       percent_->suicide ();
215       percent_counter_->suicide ();
216     }
217 }
218
219 void
220 Percent_repeat_engraver::typeset_perc ()
221 {
222   if (percent_)
223     {
224       Grob *col = first_command_column_;
225
226       percent_->set_bound (RIGHT, col);
227       percent_ = 0;
228
229       if (percent_counter_)
230         percent_counter_->set_bound (RIGHT, col);
231       percent_counter_ = 0;
232     }
233 }
234
235 bool
236 Percent_repeat_engraver::check_count_visibility (SCM count)
237 {
238   SCM proc = get_property ("repeatCountVisibility");
239   return (ly_is_procedure (proc) && to_boolean (scm_call_2 (proc,
240                                                             count,
241                                                             context ()->self_scm ())));
242 }
243
244
245 void
246 Percent_repeat_engraver::stop_translation_timestep ()
247 {
248 }
249
250 ADD_TRANSLATOR (Percent_repeat_engraver,
251                 /* doc */
252                 "Make whole bar and double bar repeats.",
253                 
254                 /* create */
255                 "DoublePercentRepeat "
256                 "DoublePercentRepeatCounter "
257                 "PercentRepeat "
258                 "PercentRepeatCounter ",
259
260                 /* read */
261                 "countPercentRepeats "
262                 "currentCommandColumn "
263                 "measureLength "
264                 "repeatCountVisibility ",
265
266                 /* write */
267                 "forbidBreak "
268                 );