2 percent-repeat-engraver.cc -- implement Percent_repeat_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>, Erik Sandberg <mandolaerik@gmail.com>
10 #include "score-engraver.hh"
12 #include "bar-line.hh"
13 #include "global-context.hh"
14 #include "international.hh"
17 #include "repeated-music.hh"
18 #include "side-position-interface.hh"
20 #include "stream-event.hh"
23 #include "translator.icc"
26 * TODO: Create separate Double_percent_repeat_engraver?
27 * Or, at least move double percent handling to Slash_repeat_engraver
30 class Percent_repeat_engraver
: public Engraver
33 bool check_count_visibility (SCM count
);
35 TRANSLATOR_DECLARATIONS (Percent_repeat_engraver
);
38 Stream_event
*percent_event_
;
40 /// moment (global time) where percent started.
50 Repeat_sign_type repeat_sign_type_
;
53 Spanner
*percent_counter_
;
56 Grob
*first_command_column_
;
57 Moment command_moment_
;
60 virtual void finalize ();
61 DECLARE_TRANSLATOR_LISTENER (percent
);
63 void stop_translation_timestep ();
64 void start_translation_timestep ();
65 void process_music ();
68 Percent_repeat_engraver::Percent_repeat_engraver ()
74 first_command_column_
= 0;
75 command_moment_
= Moment (-1);
79 Percent_repeat_engraver::start_translation_timestep ()
81 if (now_mom ().main_part_
!= command_moment_
.main_part_
)
83 first_command_column_
= unsmob_grob (get_property ("currentCommandColumn"));
84 command_moment_
= now_mom ();
87 if (stop_mom_
.main_part_
== now_mom ().main_part_
)
92 repeat_sign_type_
= UNKNOWN
;
96 IMPLEMENT_TRANSLATOR_LISTENER (Percent_repeat_engraver
, percent
);
98 Percent_repeat_engraver::listen_percent (Stream_event
*ev
)
102 Moment body_length
= get_event_length (ev
);
103 Moment
meas_len (robust_scm2moment (get_property ("measureLength"),
105 if (meas_len
== body_length
)
107 repeat_sign_type_
= MEASURE
;
108 start_mom_
= now_mom ();
109 stop_mom_
= now_mom () + body_length
;
110 get_global_context ()->add_moment_to_process (stop_mom_
);
112 else if (Moment (2) * meas_len
== body_length
)
114 repeat_sign_type_
= DOUBLE_MEASURE
;
115 start_mom_
= now_mom () + meas_len
;
116 stop_mom_
= now_mom () + body_length
; /* never used */
117 get_global_context ()->add_moment_to_process (start_mom_
);
122 don't warn about percent repeats: slash repeats are not
123 exactly 1 or 2 measures long.
130 /* print a warning: no assignment happens because
131 percent_event_ != 0 */
132 ASSIGN_EVENT_ONCE (percent_event_
, ev
);
136 Percent_repeat_engraver::process_music ()
138 if (percent_event_
&& now_mom ().main_part_
== start_mom_
.main_part_
)
140 if (repeat_sign_type_
== MEASURE
)
145 percent_
= make_spanner ("PercentRepeat", percent_event_
->self_scm ());
147 Grob
*col
= first_command_column_
;
148 percent_
->set_bound (LEFT
, col
);
150 SCM count
= percent_event_
->get_property ("repeat-count");
151 if (count
!= SCM_EOL
&& to_boolean (get_property ("countPercentRepeats"))
152 && check_count_visibility (count
))
155 = make_spanner ("PercentRepeatCounter", percent_event_
->self_scm ());
157 SCM text
= scm_number_to_string (count
, scm_from_int (10));
158 percent_counter_
->set_property ("text", text
);
159 percent_counter_
->set_bound (LEFT
, col
);
160 Side_position_interface::add_support (percent_counter_
,
162 percent_counter_
->set_parent (percent_
, Y_AXIS
);
165 percent_counter_
= 0;
167 else if (repeat_sign_type_
== DOUBLE_MEASURE
)
169 Item
*double_percent
= make_item ("DoublePercentRepeat", percent_event_
->self_scm ());
171 SCM count
= percent_event_
->get_property ("repeat-count");
172 if (count
!= SCM_EOL
&& to_boolean (get_property ("countPercentRepeats"))
173 && check_count_visibility (count
))
175 Item
*double_percent_counter
= make_item ("DoublePercentRepeatCounter",
176 percent_event_
->self_scm ());
178 SCM text
= scm_number_to_string (count
,
180 double_percent_counter
->set_property ("text", text
);
182 Side_position_interface::add_support (double_percent_counter
,
184 double_percent_counter
->set_parent (double_percent
, Y_AXIS
);
185 double_percent_counter
->set_parent (double_percent
, X_AXIS
);
188 /* forbid breaks on a % line. Should forbid all breaks, really. */
189 context ()->get_score_context ()->set_property ("forbidBreak", SCM_BOOL_T
);
191 /* No more processing needed. */
192 repeat_sign_type_
= UNKNOWN
;
198 Percent_repeat_engraver::finalize ()
202 percent_event_
->origin ()->warning (_ ("unterminated percent repeat"));
203 percent_
->suicide ();
204 percent_counter_
->suicide ();
209 Percent_repeat_engraver::typeset_perc ()
213 Grob
*col
= first_command_column_
;
215 percent_
->set_bound (RIGHT
, col
);
218 if (percent_counter_
)
219 percent_counter_
->set_bound (RIGHT
, col
);
220 percent_counter_
= 0;
225 Percent_repeat_engraver::check_count_visibility (SCM count
)
227 SCM proc
= get_property ("repeatCountVisibility");
228 return (ly_is_procedure (proc
) && to_boolean (scm_call_2 (proc
,
230 context ()->self_scm ())));
235 Percent_repeat_engraver::stop_translation_timestep ()
239 ADD_TRANSLATOR (Percent_repeat_engraver
,
241 "Make whole bar and double bar repeats.",
244 "DoublePercentRepeat "
245 "DoublePercentRepeatCounter "
247 "PercentRepeatCounter ",
250 "countPercentRepeats "
251 "currentCommandColumn "
253 "repeatCountVisibility ",