]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*
2   auto-beam-engraver.cc -- implement Auto_beam_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "engraver.hh"
10 #include "beaming-pattern.hh"
11 #include "beam.hh"
12 #include "stem.hh"
13 #include "warn.hh"
14 #include "bar-line.hh"
15 #include "rest.hh"
16 #include "item.hh"
17 #include "spanner.hh"
18 #include "context.hh"
19 #include "duration.hh"
20
21 #include "translator.icc"
22
23 class Auto_beam_engraver : public Engraver
24 {
25   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
26
27 protected:
28   void stop_translation_timestep ();
29   void start_translation_timestep ();
30   void process_music ();
31   virtual bool try_music (Music *);
32   virtual void finalize ();
33   virtual void derived_mark () const;
34
35   DECLARE_ACKNOWLEDGER (rest);
36   DECLARE_ACKNOWLEDGER (beam);
37   DECLARE_ACKNOWLEDGER (bar_line);
38   DECLARE_ACKNOWLEDGER (stem);
39
40   void process_acknowledged ();
41
42 private:
43   bool test_moment (Direction, Moment);
44   void consider_begin (Moment);
45   void consider_end (Moment);
46   Spanner *create_beam ();
47   void begin_beam ();
48   void end_beam ();
49   void junk_beam ();
50   bool is_same_grace_state (Grob *e);
51   void typeset_beam ();
52
53   Music *forbid_;
54   /*
55     shortest_mom is the shortest note in the beam.
56   */
57   Moment shortest_mom_;
58   Spanner *finished_beam_;
59   vector<Item*> *stems_;
60
61   int process_acknowledged_count_;
62   Moment last_add_mom_;
63   /*
64     Projected ending of the  beam we're working on.
65   */
66   Moment extend_mom_;
67   Moment beam_start_moment_;
68   Moment beam_start_location_;
69
70   bool subdivide_beams_;
71   Moment beat_length_;
72
73   // We act as if beam were created, and start a grouping anyway.
74   Beaming_pattern *grouping_;
75   SCM beam_settings_;
76
77   Beaming_pattern *finished_grouping_;
78
79   void check_bar_property ();
80 };
81
82 void
83 Auto_beam_engraver::derived_mark () const
84 {
85   scm_gc_mark (beam_settings_);
86 }
87
88 void
89 Auto_beam_engraver::check_bar_property ()
90 {
91   /* Duplicated from process_music (), since
92      Repeat_acknowledge_engraver::process_music () may also set whichBar.  */
93
94   Moment now = now_mom ();
95   if (scm_is_string (get_property ("whichBar"))
96       && beam_start_moment_ < now)
97     {
98       consider_end (shortest_mom_);
99       junk_beam ();
100     }
101 }
102
103 void
104 Auto_beam_engraver::process_music ()
105 {
106   if (scm_is_string (get_property ("whichBar")))
107     {
108       consider_end (shortest_mom_);
109       junk_beam ();
110     }
111
112   if (forbid_)
113     {
114       consider_end (shortest_mom_);
115       junk_beam ();
116     }
117 }
118
119 Auto_beam_engraver::Auto_beam_engraver ()
120 {
121   forbid_ = 0;
122   process_acknowledged_count_ = 0;
123   stems_ = 0;
124   shortest_mom_ = Moment (Rational (1, 8));
125   finished_beam_ = 0;
126   finished_grouping_ = 0;
127   grouping_ = 0;
128   beam_settings_ = SCM_EOL;
129 }
130
131 bool
132 Auto_beam_engraver::try_music (Music *m)
133 {
134   if (m->is_mus_type ("beam-forbid-event"))
135     {
136       forbid_ = m;
137       return true;
138     }
139
140   return false;
141 }
142
143 bool
144 Auto_beam_engraver::test_moment (Direction dir, Moment test)
145 {
146   return scm_call_3 (get_property ("autoBeamCheck"),
147                      context ()->self_scm (),
148                      scm_from_int (dir),
149                      test.smobbed_copy ())
150     != SCM_BOOL_F;
151 }
152
153 void
154 Auto_beam_engraver::consider_begin (Moment test_mom)
155 {
156   bool on = to_boolean (get_property ("autoBeaming"));
157   if (!stems_ && on
158       && !forbid_)
159     {
160       bool b = test_moment (START, test_mom);
161       if (b)
162         begin_beam ();
163     }
164 }
165
166 void
167 Auto_beam_engraver::consider_end (Moment test_mom)
168 {
169   if (stems_)
170     {
171       /* Allow already started autobeam to end:
172          don't check for autoBeaming */
173       bool b = test_moment (STOP, test_mom);
174       if (b)
175         end_beam ();
176     }
177 }
178
179 Spanner *
180 Auto_beam_engraver::create_beam ()
181 {
182   if (to_boolean (get_property ("skipTypesetting")))
183     return 0;
184
185   for (vsize i = 0; i < stems_->size (); i++)
186     if (Stem::get_beam ((*stems_)[i]))
187       return 0;
188
189   /*
190     Can't use make_spanner_from_properties() because we have to use
191     beam_settings_.
192   */
193   Spanner *beam = new Spanner (beam_settings_,
194                                context ()->get_grob_key ("Beam"));
195
196   for (vsize i = 0; i < stems_->size (); i++)
197     Beam::add_stem (beam, (*stems_)[i]);
198
199   announce_grob (beam, (*stems_)[0]->self_scm ());
200
201   return beam;
202 }
203
204 void
205 Auto_beam_engraver::begin_beam ()
206 {
207   if (stems_ || grouping_)
208     {
209       programming_error ("already have autobeam");
210       return;
211     }
212
213   stems_ = new vector<Item*>;
214   grouping_ = new Beaming_pattern ();
215   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
216
217   beam_start_moment_ = now_mom ();
218   beam_start_location_
219     = robust_scm2moment (get_property ("measurePosition"), Moment (0));
220 }
221
222 void
223 Auto_beam_engraver::junk_beam ()
224 {
225   if (!stems_)
226     return;
227
228   delete stems_;
229   stems_ = 0;
230   delete grouping_;
231   grouping_ = 0;
232   beam_settings_ = SCM_EOL;
233
234   shortest_mom_ = Moment (Rational (1, 8));
235 }
236
237 void
238 Auto_beam_engraver::end_beam ()
239 {
240   if (stems_->size () < 2)
241     junk_beam ();
242   else
243     {
244       finished_beam_ = create_beam ();
245       if (finished_beam_)
246         finished_grouping_ = grouping_;
247       delete stems_;
248       stems_ = 0;
249       grouping_ = 0;
250       beam_settings_ = SCM_EOL;
251     }
252
253   shortest_mom_ = Moment (Rational (1, 8));
254 }
255
256 void
257 Auto_beam_engraver::typeset_beam ()
258 {
259   if (finished_beam_)
260     {
261       if (!finished_beam_->get_bound (RIGHT))
262         finished_beam_->set_bound (RIGHT, finished_beam_->get_bound (LEFT));
263       
264       finished_grouping_->beamify (context ());
265       Beam::set_beaming (finished_beam_, finished_grouping_);
266       finished_beam_ = 0;
267
268       delete finished_grouping_;
269       finished_grouping_ = 0;
270     }
271 }
272
273 void
274 Auto_beam_engraver::start_translation_timestep ()
275 {
276   process_acknowledged_count_ = 0;
277   /*
278     don't beam over skips
279   */
280   if (stems_)
281     {
282       Moment now = now_mom ();
283       if (extend_mom_ < now)
284         end_beam ();
285     }
286   forbid_ = 0;
287 }
288
289 void
290 Auto_beam_engraver::stop_translation_timestep ()
291 {
292   typeset_beam ();
293 }
294
295 void
296 Auto_beam_engraver::finalize ()
297 {
298   /* finished beams may be typeset */
299   typeset_beam ();
300   /* but unfinished may need another announce/acknowledge pass */
301   if (stems_)
302     junk_beam ();
303 }
304
305
306 void
307 Auto_beam_engraver::acknowledge_beam (Grob_info info)
308 {
309   (void)info;
310   check_bar_property ();
311   if (stems_)
312     end_beam ();
313 }
314
315 void
316 Auto_beam_engraver::acknowledge_bar_line (Grob_info info)
317 {
318   (void)info;
319   check_bar_property ();
320   if (stems_)
321     end_beam ();
322 }
323
324 void
325 Auto_beam_engraver::acknowledge_rest (Grob_info info)
326 {
327   (void)info;
328   check_bar_property ();
329   if (stems_)
330     end_beam ();
331 }
332
333 void
334 Auto_beam_engraver::acknowledge_stem (Grob_info info)
335 {
336   check_bar_property ();
337   Item *stem = dynamic_cast<Item *> (info.grob ());
338   Music *m = info.ultimate_music_cause ();
339   if (!m->is_mus_type ("rhythmic-event"))
340     {
341       programming_error ("stem must have rhythmic structure");
342       return;
343     }
344
345   /*
346     Don't (start) auto-beam over empty stems; skips or rests
347   */
348   if (!Stem::head_count (stem))
349     {
350       if (stems_)
351         end_beam ();
352       return;
353     }
354
355   if (Stem::get_beam (stem))
356     {
357       if (stems_)
358         junk_beam ();
359       return;
360     }
361
362   int durlog = unsmob_duration (m->get_property ("duration"))->duration_log ();
363
364   if (durlog <= 2)
365     {
366       if (stems_)
367         end_beam ();
368       return;
369     }
370
371   /*
372     ignore grace notes.
373   */
374   Moment now = now_mom ();
375   if (bool (beam_start_location_.grace_part_) != bool (now.grace_part_))
376     return;
377
378   Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
379
380   consider_end (dur);
381   consider_begin (dur);
382
383   if (dur < shortest_mom_)
384     shortest_mom_ = dur;
385
386   if (!stems_)
387     return;
388
389   grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
390                        durlog - 2);
391   stems_->push_back (stem);
392   last_add_mom_ = now;
393   extend_mom_ = max (extend_mom_, now) + m->get_length ();
394 }
395
396 void
397 Auto_beam_engraver::process_acknowledged ()
398 {
399   if (extend_mom_ > now_mom ())
400     return;
401
402   if (!process_acknowledged_count_)
403     {
404       consider_end (shortest_mom_);
405       consider_begin (shortest_mom_);
406     }
407   else if (process_acknowledged_count_ > 1)
408     {
409       if (stems_)
410         {
411           Moment now = now_mom ();
412           if ((extend_mom_ < now)
413               || ((extend_mom_ == now) && (last_add_mom_ != now)))
414             end_beam ();
415           else if (!stems_->size ())
416             junk_beam ();
417         }
418     }
419
420   process_acknowledged_count_++;
421 }
422
423 ADD_ACKNOWLEDGER (Auto_beam_engraver, stem);
424 ADD_ACKNOWLEDGER (Auto_beam_engraver, bar_line);
425 ADD_ACKNOWLEDGER (Auto_beam_engraver, beam);
426 ADD_ACKNOWLEDGER (Auto_beam_engraver, rest);
427 ADD_TRANSLATOR (Auto_beam_engraver,
428                 /* doc */ "Generate beams based on measure characteristics and observed "
429                 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
430                 "when to start and stop a beam.  Overriding beaming is done through "
431                 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
432                 "@code{stemRightBeamCount}. ",
433                 /* create */ "Beam",
434                 /* accept */ "beam-forbid-event",
435                 /* read */ "autoBeaming autoBeamSettings beatLength subdivideBeams",
436                 /* write */ "");