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