]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / beam-engraver.cc
1 /*
2   beam-engraver.cc -- implement Beam_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "beam.hh"
10 #include "beaming-pattern.hh"
11 #include "context.hh"
12 #include "drul-array.hh"
13 #include "duration.hh"
14 #include "engraver.hh"
15 #include "international.hh"
16 #include "item.hh"
17 #include "rest.hh"
18 #include "score-context.hh"
19 #include "spanner.hh"
20 #include "stem.hh"
21 #include "warn.hh"
22
23 #include "translator.icc"
24
25 class Beam_engraver : public Engraver
26 {
27 public:
28   DECLARE_ACKNOWLEDGER (stem);
29   DECLARE_ACKNOWLEDGER (rest);
30 protected:
31   Music *start_ev_;
32
33   Spanner *finished_beam_;
34   Spanner *beam_;
35   Music *prev_start_ev_;
36
37   Music *now_stop_ev_;
38
39   Beaming_pattern *beam_info_;
40   Beaming_pattern *finished_beam_info_;
41
42   /// location  within measure where beam started.
43   Moment beam_start_location_;
44
45   /// moment (global time) where beam started.
46   Moment beam_start_mom_;
47
48   bool subdivide_beams_;
49   Moment beat_length_;
50
51   void typeset_beam ();
52   void set_melisma (bool);
53
54   Moment last_stem_added_at_;
55   void stop_translation_timestep ();
56   void start_translation_timestep ();
57   virtual void finalize ();
58
59   virtual bool try_music (Music *);
60   void process_music ();
61
62   virtual bool valid_start_point ();
63   virtual bool valid_end_point ();
64
65 public:
66   TRANSLATOR_DECLARATIONS (Beam_engraver);
67 };
68
69 /*
70   Hmm. this isn't necessary, since grace beams and normal beams are
71   always nested.
72 */
73 bool
74 Beam_engraver::valid_start_point ()
75 {
76   Moment n = now_mom ();
77
78   return n.grace_part_ == Rational (0);
79 }
80
81 bool
82 Beam_engraver::valid_end_point ()
83 {
84   return valid_start_point ();
85 }
86
87 Beam_engraver::Beam_engraver ()
88 {
89   beam_ = 0;
90   finished_beam_ = 0;
91   finished_beam_info_ = 0;
92   beam_info_ = 0;
93   now_stop_ev_ = 0;
94   start_ev_ = 0;
95   prev_start_ev_ = 0;
96 }
97
98 bool
99 Beam_engraver::try_music (Music *m)
100 {
101   if (m->is_mus_type ("beam-event"))
102     {
103       Direction d = to_dir (m->get_property ("span-direction"));
104       if (d == START && !valid_start_point ())
105         return false;
106       if (d == STOP && !valid_end_point ())
107         return false;
108
109       if (d == START)
110         start_ev_ = m;
111       else if (d == STOP)
112         now_stop_ev_ = m;
113       return true;
114     }
115   return false;
116 }
117
118 void
119 Beam_engraver::set_melisma (bool ml)
120 {
121   SCM b = get_property ("autoBeaming");
122   if (!to_boolean (b))
123     context ()->set_property ("beamMelismaBusy", ml ? SCM_BOOL_T : SCM_BOOL_F);
124 }
125
126 void
127 Beam_engraver::process_music ()
128 {
129   if (beam_ && !to_boolean (get_property ("allowBeamBreak")))
130     context ()->get_score_context ()->set_property ("forbidBreak", SCM_BOOL_T);
131
132   if (start_ev_)
133     {
134       if (beam_)
135         {
136           start_ev_->origin ()->warning (_ ("already have a beam"));
137           return;
138         }
139
140       set_melisma (true);
141       prev_start_ev_ = start_ev_;
142       beam_ = make_spanner ("Beam", start_ev_->self_scm ());
143       Moment mp (robust_scm2moment (get_property ("measurePosition"), Moment (0)));
144
145       beam_start_location_ = mp;
146       beam_start_mom_ = now_mom ();
147
148       beam_info_ = new Beaming_pattern;
149       /* urg, must copy to Auto_beam_engraver too */
150     }
151 }
152
153 void
154 Beam_engraver::typeset_beam ()
155 {
156   if (finished_beam_)
157     {
158       if (!finished_beam_->get_bound (RIGHT))
159         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
160           
161       finished_beam_info_->beamify (context ());
162       Beam::set_beaming (finished_beam_, finished_beam_info_);
163
164       delete finished_beam_info_;
165       finished_beam_info_ = 0;
166       finished_beam_ = 0;
167     }
168 }
169
170 void
171 Beam_engraver::start_translation_timestep ()
172 {
173   start_ev_ = 0;
174
175   if (beam_)
176     {
177       set_melisma (true);
178     }
179 }
180
181 void
182 Beam_engraver::stop_translation_timestep ()
183 {
184   typeset_beam ();
185   if (now_stop_ev_)
186     {
187       finished_beam_ = beam_;
188       finished_beam_info_ = beam_info_;
189
190       now_stop_ev_ = 0;
191       beam_ = 0;
192       beam_info_ = 0;
193       typeset_beam ();
194       set_melisma (false);
195     }
196 }
197
198 void
199 Beam_engraver::finalize ()
200 {
201   typeset_beam ();
202   if (beam_)
203     {
204       prev_start_ev_->origin ()->warning (_ ("unterminated beam"));
205
206       /*
207         we don't typeset it, (we used to, but it was commented
208         out. Reason unknown) */
209       beam_->suicide ();
210       delete beam_info_;
211     }
212 }
213
214 void
215 Beam_engraver::acknowledge_rest (Grob_info info)
216 {
217   if (beam_)
218     {
219       chain_offset_callback (info.grob(),
220                              Beam::rest_collision_callback_proc, Y_AXIS);
221     }
222 }
223
224
225
226 void
227 Beam_engraver::acknowledge_stem (Grob_info info)
228 {
229   if (!beam_)
230     return;
231   
232   Moment now = now_mom ();
233   if (!valid_start_point ())
234     return;
235
236   Item *stem = dynamic_cast<Item *> (info.grob ());
237   if (Stem::get_beam (stem))
238     return;
239
240   
241   
242   Music *m = info.ultimate_music_cause ();
243   if (!m->is_mus_type ("rhythmic-event"))
244     {
245       string s = _ ("stem must have Rhythmic structure");
246       if (info.music_cause ())
247         info.music_cause ()->origin ()->warning (s);
248       else
249         ::warning (s);
250
251       return;
252     }
253
254   last_stem_added_at_ = now;
255   int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
256   if (durlog <= 2)
257     {
258       m->origin ()->warning (_ ("stem doesn't fit in beam"));
259       prev_start_ev_->origin ()->warning (_ ("beam was started here"));
260       /*
261         don't return, since
262
263         [r4 c8] can just as well be modern notation.
264       */
265     }
266
267   stem->set_property ("duration-log",
268                       scm_from_int (durlog));
269   Moment stem_location = now - beam_start_mom_ + beam_start_location_;
270   beam_info_->add_stem (stem_location,
271                         max (durlog- 2, 0));
272   Beam::add_stem (beam_, stem);
273 }
274
275 ADD_ACKNOWLEDGER (Beam_engraver, stem);
276 ADD_ACKNOWLEDGER (Beam_engraver, rest);
277
278 ADD_TRANSLATOR (Beam_engraver,
279                 /* doc */
280
281                 "Handles Beam events by engraving Beams.  If omitted, then notes will be "
282                 "printed with flags instead of beams.",
283                 
284                 /* create */ "Beam",
285                 /* accept */ "beam-event",
286
287                 /* read */
288                 "beamMelismaBusy "
289                 "beatLength "
290                 "subdivideBeams "
291                 ,
292                 /* write */
293                 "forbidBreak");
294
295 class Grace_beam_engraver : public Beam_engraver
296 {
297 public:
298   TRANSLATOR_DECLARATIONS (Grace_beam_engraver);
299
300 protected:
301   virtual bool valid_start_point ();
302   virtual bool valid_end_point ();
303 };
304
305 Grace_beam_engraver::Grace_beam_engraver ()
306 {
307 }
308
309 bool
310 Grace_beam_engraver::valid_start_point ()
311 {
312   Moment n = now_mom ();
313
314   return n.grace_part_ != Rational (0);
315 }
316
317 bool
318 Grace_beam_engraver::valid_end_point ()
319 {
320   return beam_ && valid_start_point ();
321 }
322
323 ADD_ACKNOWLEDGER (Grace_beam_engraver, stem);
324 ADD_ACKNOWLEDGER (Grace_beam_engraver, rest);
325
326 ADD_TRANSLATOR (Grace_beam_engraver,
327
328                 /* doc */
329
330                 "Handles Beam events by engraving Beams.  If omitted, then notes will "
331                 "be printed with flags instead of beams. Only engraves beams when we "
332                 " are at grace points in time. ",
333                 
334                 /* create */ "Beam",
335                 /* accept */ "beam-event",
336                 /* read */
337                 "beamMelismaBusy "
338                 "beatLength "
339                 "allowBeamBreak "
340                 "subdivideBeams "
341                 ,
342                 /* write */ "");
343