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