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