]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
release: 1.1.43
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "beam-engraver.hh"
11 #include "musical-request.hh"
12 #include "beam.hh"
13 #include "rhythmic-grouping.hh"
14 #include "stem.hh"
15 #include "warn.hh"
16 #include "time-description.hh"
17
18 Beam_engraver::Beam_engraver ()
19 {
20   beam_p_ = 0;
21   finished_beam_p_ =0;
22   finished_grouping_p_ = 0;
23   grouping_p_ =0;
24   reqs_drul_[LEFT] = reqs_drul_[RIGHT] =0;
25   prev_start_req_ =0;
26 }
27
28 bool
29 Beam_engraver::do_try_music (Music *m)
30 {
31   if (Span_req * c = dynamic_cast<Span_req*>(m))
32     {
33       if (c->span_type_str_ != "beam")
34         return false;
35       
36       Direction d =c->span_dir_;
37
38       if (d == STOP && !beam_p_)
39         {
40           m->warning (_ ("No Beam to end"));
41           return false;
42         }
43       reqs_drul_[d ] = c;
44       return true;
45     }
46   return false;
47 }
48
49
50 void
51 Beam_engraver::do_process_requests ()
52 {
53   if (reqs_drul_[STOP])
54     {
55       if (!beam_p_)
56         reqs_drul_[STOP]->warning (_("No beam to end"));
57       prev_start_req_ =0;
58       finished_beam_p_ = beam_p_;
59       finished_grouping_p_ = grouping_p_;
60
61       beam_p_ = 0;
62       grouping_p_ = 0;
63     }
64   
65   if (reqs_drul_[START])
66     {
67       if (beam_p_)
68         {
69           reqs_drul_[START]->warning (_ ("Already have a Beam"));
70           return;
71         }
72
73       prev_start_req_ = reqs_drul_[START];
74       beam_p_ = new Beam;
75       grouping_p_ = new Rhythmic_grouping;
76
77       Scalar prop = get_property ("beamslopedamping", 0);
78       if (prop.isnum_b ()) 
79         beam_p_->set_elt_property (damping_scm_sym, gh_int2scm( prop));
80       
81       prop = get_property ("beamquantisation", 0);
82       if (prop.isnum_b ()) 
83         beam_p_->quantisation_ = (Beam::Quantisation)(int)prop;
84  
85       announce_element (Score_element_info (beam_p_, reqs_drul_[START]));
86     }
87 }
88
89 void
90 Beam_engraver::typeset_beam ()
91 {
92   if (finished_beam_p_)
93     {
94       Rhythmic_grouping const * rg_C = get_staff_info().rhythmic_C_;
95       rg_C->extend (finished_grouping_p_->interval());
96       finished_beam_p_->set_grouping (*rg_C, *finished_grouping_p_);
97       typeset_element (finished_beam_p_);
98       finished_beam_p_ = 0;
99     
100       delete finished_grouping_p_;
101       finished_grouping_p_= 0;
102     
103       reqs_drul_[STOP] = 0;
104     }
105 }
106
107 void
108 Beam_engraver::do_post_move_processing ()
109 {
110   reqs_drul_ [START] =0;
111 }
112
113 void
114 Beam_engraver::do_pre_move_processing ()
115 {
116   typeset_beam ();
117 }
118
119 void
120 Beam_engraver::do_removal_processing ()
121 {
122   typeset_beam ();
123   if (beam_p_)
124     {
125       prev_start_req_->warning (_ ("Unfinished beam"));
126       finished_beam_p_ = beam_p_;
127       finished_grouping_p_ = grouping_p_;
128       typeset_beam ();
129     }
130 }
131
132 void
133 Beam_engraver::acknowledge_element (Score_element_info info)
134 {
135     if (beam_p_)
136       {
137         Stem* stem_l = dynamic_cast<Stem *> (info.elem_l_);
138         if (!stem_l)
139           return;
140
141
142         Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
143         if (!rhythmic_req)
144           {
145             String s = _ ("Stem must have Rhythmic structure.");
146             if (info.req_l_)
147               info.req_l_->warning (s);
148             else
149               ::warning (s);
150           
151             return;
152           }
153       
154
155         if (rhythmic_req->duration_.durlog_i_<= 2)
156           {
157             rhythmic_req->warning (_ ("stem doesn't fit in beam"));
158             prev_start_req_->warning (_ ("beam was started here"));
159             return;
160           }
161
162         /*
163           TODO: do something sensible if it doesn't fit in the beam.
164         */
165         Moment start = get_staff_info().time_C_->whole_in_measure_;
166
167         if (!grouping_p_->child_fit_b (start))
168           {
169             String s (_ ("please fix me") + ": " 
170                       + _f ("stem at %s doesn't fit in beam", now_mom ().str ()));
171
172             if (info.req_l_)
173               info.req_l_->warning(s);
174             else 
175               warning (s);
176           }
177         else
178           {
179             grouping_p_->add_child (start, rhythmic_req->length_mom ());
180             stem_l->flag_i_ = rhythmic_req->duration_.durlog_i_;
181             beam_p_->add_stem (stem_l);
182           }
183       }
184 }
185
186
187
188 ADD_THIS_TRANSLATOR(Beam_engraver);