]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
patch::: 1.3.108.jcn4
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "engraver-group-engraver.hh"
10 #include "engraver.hh"
11 #include "musical-request.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "beaming.hh"
16 #include "score-engraver.hh"
17 #include "rest.hh"
18 #include "drul-array.hh"
19 #include "item.hh"
20 #include "spanner.hh"
21
22 class Beam_engraver : public Engraver
23 {
24   Drul_array<Span_req*> reqs_drul_;
25
26   Link_array<Stem> stems_;
27   
28   
29   Spanner *finished_beam_p_;
30   Spanner *beam_p_;
31   Span_req * prev_start_req_;
32
33   Beaming_info_list * beam_info_p_;
34   Beaming_info_list * finished_beam_info_p_;  
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   void typeset_beam ();
43   void set_melisma (bool);
44 protected:
45   virtual void do_pre_move_processing ();
46   virtual void do_post_move_processing ();
47   virtual void do_removal_processing ();
48   virtual void process_acknowledged ();
49   virtual void acknowledge_element (Score_element_info);
50   virtual bool do_try_music (Music*);
51   void deprecated_process_music ();
52 public:
53   Beam_engraver ();
54   VIRTUAL_COPY_CONS (Translator);
55 };
56
57
58 Beam_engraver::Beam_engraver ()
59 {
60   beam_p_ = 0;
61   finished_beam_p_ =0;
62   finished_beam_info_p_=0;
63   beam_info_p_ =0;
64   reqs_drul_[LEFT] = reqs_drul_[RIGHT] =0;
65   prev_start_req_ =0;
66 }
67
68 bool
69 Beam_engraver::do_try_music (Music *m)
70 {
71   if (Span_req * c = dynamic_cast<Span_req*>(m))
72     {
73       if (scm_equal_p (c->get_mus_property ("span-type"),
74                        ly_str02scm ("abort")) == SCM_BOOL_T)
75         {
76           reqs_drul_[START] = 0;
77           reqs_drul_[STOP] = 0;
78           if (beam_p_)
79             beam_p_->suicide ();
80           beam_p_ = 0;
81         }
82       else if (scm_equal_p (c->get_mus_property ("span-type"),
83                        ly_str02scm ("beam")) == SCM_BOOL_T)
84         {
85       
86           Direction d =c->get_span_dir ();
87
88           if (d == STOP && !beam_p_)
89             {
90               m->origin ()->warning  (_ ("can't find start of beam"));
91               return false;
92             }
93
94           if(d == STOP)
95             {
96               SCM m = get_property ("automaticMelismata");
97               SCM b = get_property("noAutoBeaming");
98               if (to_boolean (m) && to_boolean(b))
99                 {
100                   set_melisma (false);
101                 }
102             }
103
104           reqs_drul_[d ] = c;
105           return true;
106         }
107     }
108   return false;
109 }
110
111 void
112 Beam_engraver::set_melisma (bool m)
113 {
114   daddy_trans_l_->set_property ("beamMelismaBusy", m ? SCM_BOOL_T :SCM_BOOL_F);
115 }
116
117
118 void
119 Beam_engraver::deprecated_process_music ()
120 {
121   if (reqs_drul_[STOP])
122     {
123       if (!beam_p_)
124         reqs_drul_[STOP]->origin ()->warning (_("can't find start of beam"));
125       prev_start_req_ =0;
126       finished_beam_p_ = beam_p_;
127       finished_beam_info_p_ = beam_info_p_;
128
129       beam_info_p_ =0;
130       beam_p_ = 0;
131     }
132
133
134   if (beam_p_ && !to_boolean (get_property ("weAreGraceContext")))
135     {
136       Score_engraver * e = 0;
137       Translator * t  =  daddy_grav_l ();
138       for (; !e && t;  t = t->daddy_trans_l_)
139         {
140           e = dynamic_cast<Score_engraver*> (t);
141         }
142       
143       if (!e)
144         programming_error ("No score engraver!");
145       else
146         e->forbid_breaks ();
147     }
148   
149   if (reqs_drul_[START])
150     {
151       if (beam_p_)
152         {
153           reqs_drul_[START]->origin ()->warning (_ ("already have a beam"));
154           return;
155         }
156
157       prev_start_req_ = reqs_drul_[START];
158       beam_p_ = new Spanner (get_property ("Beam"));
159       Beam::set_interface (beam_p_);
160       
161       SCM smp = get_property ("measurePosition");
162       Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
163
164       beam_start_location_ = mp;
165       beam_start_mom_ = now_mom();
166       beam_info_p_ = new Beaming_info_list;
167       
168       
169       /* urg, must copy to Auto_beam_engraver too */
170  
171       announce_element (beam_p_, reqs_drul_[START]);
172     }
173   reqs_drul_[STOP] = 0;
174   reqs_drul_[START] = 0;
175 }
176
177 void
178 Beam_engraver::typeset_beam ()
179 {
180   if (finished_beam_p_)
181     {
182       finished_beam_info_p_->beamify ();
183       
184       Beam::set_beaming (finished_beam_p_, finished_beam_info_p_);
185       typeset_element (finished_beam_p_);
186       delete finished_beam_info_p_;
187       finished_beam_info_p_ =0;
188       finished_beam_p_ = 0;
189     
190       reqs_drul_[STOP] = 0;
191     }
192 }
193
194 void
195 Beam_engraver::do_post_move_processing ()
196 {
197   reqs_drul_ [START] =0;
198   if(beam_p_) {
199     SCM m = get_property ("automaticMelismata");
200     SCM b = get_property("noAutoBeaming");
201     if (to_boolean (m) && to_boolean(b)) {
202       set_melisma (true);
203     }
204   }
205 }
206
207 void
208 Beam_engraver::do_pre_move_processing ()
209 {
210   typeset_beam ();
211 }
212
213 void
214 Beam_engraver::do_removal_processing ()
215 {
216   typeset_beam ();
217   if (beam_p_)
218     {
219       prev_start_req_->origin ()->warning (_ ("unterminated beam"));
220 #if 0
221       finished_beam_p_ = beam_p_;
222       finished_beam_info_p_ = beam_info_p_;
223       typeset_beam ();
224 #else
225       beam_p_->suicide ();
226       delete beam_info_p_;
227 #endif
228     }
229 }
230
231 void
232 Beam_engraver::process_acknowledged ()
233 {
234   deprecated_process_music ();
235 }
236
237
238 void
239 Beam_engraver::acknowledge_element (Score_element_info info)
240 {
241   if (beam_p_)
242     {
243       if (Rest::has_interface (info.elem_l_))
244         {
245           info.elem_l_->add_offset_callback (Beam::rest_collision_callback_proc, Y_AXIS);
246         }
247       else if (Stem::has_interface (info.elem_l_))
248         {
249           Item *stem_l = dynamic_cast<Item*> (info.elem_l_);
250           if (Stem::beam_l (stem_l))
251             return;
252
253           bool stem_grace = stem_l->get_elt_property ("grace") == SCM_BOOL_T;
254
255           SCM wg =get_property ("weAreGraceContext");
256           bool wgb= to_boolean (wg);
257
258           if (wgb!= stem_grace)
259             return;
260
261           Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
262           if (!rhythmic_req)
263             {
264               String s = _ ("stem must have Rhythmic structure");
265               if (info.req_l_)
266                 info.req_l_->origin ()->warning (s);
267               else
268                 ::warning (s);
269           
270               return;
271             }
272
273       int durlog  = unsmob_duration (rhythmic_req->get_mus_property ("duration"))-> duration_log ();
274           if (durlog <= 2)
275             {
276               rhythmic_req->origin ()->warning (_ ("stem doesn't fit in beam"));
277               prev_start_req_->origin ()->warning (_ ("beam was started here"));
278               /*
279                 don't return, since
280
281                 [r4 c8] can just as well be modern notation.
282               */
283             }
284
285           stem_l->set_elt_property ("duration-log",
286                                     gh_int2scm (durlog));
287           Moment stem_location = now_mom () - beam_start_mom_ + beam_start_location_;
288           beam_info_p_->add_stem (stem_location,
289                                   (durlog- 2) >? 1);
290           Beam::add_stem (beam_p_, stem_l);
291         }
292     }
293 }
294
295
296
297 ADD_THIS_TRANSLATOR(Beam_engraver);
298