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