Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / beam-engraver.cc
blob04e571f68a953d0197908dc3a75386cd6e7eecc7
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "beam.hh"
21 #include "beaming-pattern.hh"
22 #include "context.hh"
23 #include "directional-element-interface.hh"
24 #include "drul-array.hh"
25 #include "duration.hh"
26 #include "engraver.hh"
27 #include "international.hh"
28 #include "item.hh"
29 #include "rest.hh"
30 #include "spanner.hh"
31 #include "stream-event.hh"
32 #include "stem.hh"
33 #include "warn.hh"
35 #include "translator.icc"
37 class Beam_engraver : public Engraver
39 public:
40 DECLARE_ACKNOWLEDGER (stem);
41 DECLARE_ACKNOWLEDGER (rest);
43 protected:
44 Stream_event *start_ev_;
46 Spanner *finished_beam_;
47 Spanner *beam_;
48 Stream_event *prev_start_ev_;
50 Stream_event *stop_ev_;
52 Direction forced_direction_;
54 Beaming_pattern *beam_info_;
55 Beaming_pattern *finished_beam_info_;
57 /// location within measure where beam started.
58 Moment beam_start_location_;
60 /// moment (global time) where beam started.
61 Moment beam_start_mom_;
63 Beaming_options beaming_options_;
64 Beaming_options finished_beaming_options_;
66 void typeset_beam ();
67 void set_melisma (bool);
69 Moment last_stem_added_at_;
70 void stop_translation_timestep ();
71 void start_translation_timestep ();
72 virtual void finalize ();
74 void process_music ();
76 virtual bool valid_start_point ();
77 virtual bool valid_end_point ();
79 DECLARE_TRANSLATOR_LISTENER (beam);
80 public:
81 TRANSLATOR_DECLARATIONS (Beam_engraver);
85 Hmm. this isn't necessary, since grace beams and normal beams are
86 always nested.
88 bool
89 Beam_engraver::valid_start_point ()
91 Moment n = now_mom ();
93 return n.grace_part_ == Rational (0);
96 bool
97 Beam_engraver::valid_end_point ()
99 return valid_start_point ();
102 Beam_engraver::Beam_engraver ()
104 beam_ = 0;
105 finished_beam_ = 0;
106 finished_beam_info_ = 0;
107 beam_info_ = 0;
108 forced_direction_ = CENTER;
109 stop_ev_ = 0;
110 start_ev_ = 0;
111 prev_start_ev_ = 0;
114 IMPLEMENT_TRANSLATOR_LISTENER (Beam_engraver, beam);
115 void
116 Beam_engraver::listen_beam (Stream_event *ev)
118 Direction d = to_dir (ev->get_property ("span-direction"));
120 if (d == START && valid_start_point ())
122 ASSIGN_EVENT_ONCE (start_ev_, ev);
124 Direction updown = to_dir (ev->get_property ("direction"));
125 if (updown)
126 forced_direction_ = updown;
128 else if (d == STOP && valid_end_point ())
129 ASSIGN_EVENT_ONCE (stop_ev_, ev);
132 void
133 Beam_engraver::set_melisma (bool ml)
135 SCM b = get_property ("autoBeaming");
136 if (!to_boolean (b))
137 context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
140 void
141 Beam_engraver::process_music ()
143 if (start_ev_)
145 if (beam_)
147 start_ev_->origin ()->warning (_ ("already have a beam"));
148 return;
151 set_melisma (true);
152 prev_start_ev_ = start_ev_;
153 beam_ = make_spanner ("Beam", start_ev_->self_scm ());
155 Moment mp (robust_scm2moment (get_property ("measurePosition"),
156 Moment (0)));
158 beam_start_location_ = mp;
159 beam_start_mom_ = now_mom ();
161 beaming_options_.from_context (context ());
162 beam_info_ = new Beaming_pattern;
163 /* urg, must copy to Auto_beam_engraver too */
166 typeset_beam ();
167 if (stop_ev_ && beam_)
169 announce_end_grob (beam_, stop_ev_->self_scm ());
174 void
175 Beam_engraver::typeset_beam ()
177 if (finished_beam_)
179 if (!finished_beam_->get_bound (RIGHT))
180 finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
181 if (forced_direction_)
183 Grob *stem = finished_beam_->get_bound (RIGHT);
184 set_grob_direction (stem, forced_direction_);
185 forced_direction_ = CENTER;
187 finished_beam_info_->beamify (finished_beaming_options_);
189 Beam::set_beaming (finished_beam_, finished_beam_info_);
191 delete finished_beam_info_;
192 finished_beam_info_ = 0;
193 finished_beam_ = 0;
198 void
199 Beam_engraver::start_translation_timestep ()
201 start_ev_ = 0;
203 if (beam_)
204 set_melisma (true);
207 void
208 Beam_engraver::stop_translation_timestep ()
210 if (stop_ev_)
212 finished_beam_ = beam_;
213 finished_beam_info_ = beam_info_;
214 finished_beaming_options_ = beaming_options_;
216 stop_ev_ = 0;
217 beam_ = 0;
218 beam_info_ = 0;
219 typeset_beam ();
220 set_melisma (false);
224 void
225 Beam_engraver::finalize ()
227 typeset_beam ();
228 if (beam_)
230 prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
233 we don't typeset it, (we used to, but it was commented
234 out. Reason unknown) */
235 beam_->suicide ();
236 delete beam_info_;
240 void
241 Beam_engraver::acknowledge_rest (Grob_info info)
243 if (beam_
244 && !scm_is_number (info.grob ()->get_property_data ("staff-position")))
245 chain_offset_callback (info.grob (),
246 Beam::rest_collision_callback_proc, Y_AXIS);
249 void
250 Beam_engraver::acknowledge_stem (Grob_info info)
252 if (!beam_)
253 return;
255 Moment now = now_mom ();
256 if (!valid_start_point ())
257 return;
259 Item *stem = dynamic_cast<Item *> (info.grob ());
260 if (Stem::get_beam (stem))
261 return;
263 Stream_event *ev = info.ultimate_event_cause ();
264 if (!ev->in_event_class ("rhythmic-event"))
266 info.grob ()->warning (_ ("stem must have Rhythmic structure"));
267 return;
270 last_stem_added_at_ = now;
271 int durlog = unsmob_duration (ev->get_property ("duration"))->duration_log ();
272 if (durlog <= 2)
274 ev->origin ()->warning (_ ("stem does not fit in beam"));
275 prev_start_ev_->origin ()->warning (_ ("beam was started here"));
277 don't return, since
279 [r4 c8] can just as well be modern notation.
283 if (forced_direction_)
284 set_grob_direction (stem, forced_direction_);
286 stem->set_property ("duration-log", scm_from_int (durlog));
287 Moment stem_location = now - beam_start_mom_ + beam_start_location_;
288 beam_info_->add_stem (stem_location,
289 max (durlog- 2, 0),
290 Stem::is_invisible (stem));
291 Beam::add_stem (beam_, stem);
294 ADD_ACKNOWLEDGER (Beam_engraver, stem);
295 ADD_ACKNOWLEDGER (Beam_engraver, rest);
297 ADD_TRANSLATOR (Beam_engraver,
298 /* doc */
299 "Handle @code{Beam} events by engraving beams. If omitted,"
300 " then notes are printed with flags instead of beams.",
302 /* create */
303 "Beam ",
305 /* read */
306 "baseMoment "
307 "beamMelismaBusy "
308 "beatStructure "
309 "subdivideBeams ",
311 /* write */
312 "forbidBreak"
315 class Grace_beam_engraver : public Beam_engraver
317 public:
318 TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
320 DECLARE_TRANSLATOR_LISTENER (beam);
322 protected:
323 virtual bool valid_start_point ();
324 virtual bool valid_end_point ();
327 Grace_beam_engraver::Grace_beam_engraver ()
331 bool
332 Grace_beam_engraver::valid_start_point ()
334 Moment n = now_mom ();
336 return n.grace_part_ != Rational (0);
339 bool
340 Grace_beam_engraver::valid_end_point ()
342 return beam_ && valid_start_point ();
346 Ugh, C&P code.
348 IMPLEMENT_TRANSLATOR_LISTENER (Grace_beam_engraver, beam);
349 void
350 Grace_beam_engraver::listen_beam (Stream_event *ev)
352 Direction d = to_dir (ev->get_property ("span-direction"));
354 if (d == START && valid_start_point ())
355 start_ev_ = ev;
356 else if (d == STOP && valid_end_point ())
357 stop_ev_ = ev;
361 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
362 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
364 ADD_TRANSLATOR (Grace_beam_engraver,
365 /* doc */
366 "Handle @code{Beam} events by engraving beams. If omitted,"
367 " then notes are printed with flags instead of beams. Only"
368 " engraves beams when we are at grace points in time.",
370 /* create */
371 "Beam ",
373 /* read */
374 "baseMoment "
375 "beamMelismaBusy "
376 "beatStructure "
377 "subdivideBeams ",
379 /* write */