]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-engraver.cc
release: 1.1.36
[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 (Beam_req * c = dynamic_cast<Beam_req*>(m))
32     {
33       Direction d =c->spantype_;
34
35       if (d == STOP && !beam_p_)
36         {
37           m->warning (_ ("No Beam to end"));
38           return false;
39         }
40       reqs_drul_[d ] = c;
41       return true;
42     }
43   return false;
44 }
45
46
47 void
48 Beam_engraver::do_process_requests ()
49 {
50   if (reqs_drul_[STOP])
51     {
52       if (!beam_p_)
53         reqs_drul_[STOP]->warning (_("No beam to end"));
54       prev_start_req_ =0;
55       finished_beam_p_ = beam_p_;
56       finished_grouping_p_ = grouping_p_;
57
58       beam_p_ = 0;
59       grouping_p_ = 0;
60     }
61   
62   if (reqs_drul_[START])
63     {
64       if (beam_p_)
65         {
66           reqs_drul_[START]->warning (_ ("Already have a Beam"));
67           return;
68         }
69
70       prev_start_req_ = reqs_drul_[START];
71       beam_p_ = new Beam;
72       grouping_p_ = new Rhythmic_grouping;
73
74       Scalar prop = get_property ("beamslopedamping", 0);
75       if (prop.isnum_b ()) 
76         beam_p_->set_elt_property (damping_scm_sym, gh_int2scm( prop));
77       
78       prop = get_property ("beamquantisation", 0);
79       if (prop.isnum_b ()) 
80         beam_p_->quantisation_ = (Beam::Quantisation)(int)prop;
81  
82       // must set minVerticalAlign == maxVerticalAlign to get sane results
83       // see input/test/beam-interstaff.ly
84       prop = get_property ("minVerticalAlign", 0);
85       if (prop.isnum_b ())
86         beam_p_->vertical_align_drul_[MIN] = prop;
87
88       prop = get_property ("maxVerticalAlign", 0);
89       if (prop.isnum_b ())
90         beam_p_->vertical_align_drul_[MAX] = prop;
91
92       announce_element (Score_element_info (beam_p_, reqs_drul_[START]));
93     }
94 }
95
96 void
97 Beam_engraver::typeset_beam ()
98 {
99   if (finished_beam_p_)
100     {
101       Rhythmic_grouping const * rg_C = get_staff_info().rhythmic_C_;
102       rg_C->extend (finished_grouping_p_->interval());
103       finished_beam_p_->set_grouping (*rg_C, *finished_grouping_p_);
104       typeset_element (finished_beam_p_);
105       finished_beam_p_ = 0;
106     
107       delete finished_grouping_p_;
108       finished_grouping_p_= 0;
109     
110       reqs_drul_[STOP] = 0;
111     }
112 }
113
114 void
115 Beam_engraver::do_post_move_processing ()
116 {
117   reqs_drul_ [START] =0;
118 }
119
120 void
121 Beam_engraver::do_pre_move_processing ()
122 {
123   typeset_beam ();
124 }
125
126 void
127 Beam_engraver::do_removal_processing ()
128 {
129   typeset_beam ();
130   if (beam_p_)
131     {
132       prev_start_req_->warning (_ ("Unfinished beam"));
133       finished_beam_p_ = beam_p_;
134       finished_grouping_p_ = grouping_p_;
135       typeset_beam ();
136     }
137 }
138
139 void
140 Beam_engraver::acknowledge_element (Score_element_info info)
141 {
142     if (beam_p_)
143       {
144         Stem* stem_l = dynamic_cast<Stem *> (info.elem_l_);
145         if (!stem_l)
146           return;
147
148
149         Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
150         if (!rhythmic_req)
151           {
152             String s = _ ("Stem must have Rhythmic structure.");
153             if (info.req_l_)
154               info.req_l_->warning (s);
155             else
156               ::warning (s);
157           
158             return;
159           }
160       
161
162         if (rhythmic_req->duration_.durlog_i_<= 2)
163           {
164             rhythmic_req->warning (_ ("stem doesn't fit in beam"));
165             prev_start_req_->warning (_ ("beam was started here"));
166             return;
167           }
168
169         /*
170           TODO: do something sensible if it doesn't fit in the beam.
171         */
172         Moment start = get_staff_info().time_C_->whole_in_measure_;
173
174         if (!grouping_p_->child_fit_b (start))
175           {
176             String s (_ ("please fix me") + ": " 
177                       + _f ("stem at %s doesn't fit in beam", now_mom ().str ()));
178
179             if (info.req_l_)
180               info.req_l_->warning(s);
181             else 
182               warning (s);
183           }
184         else
185           {
186             grouping_p_->add_child (start, rhythmic_req->length_mom ());
187             stem_l->flag_i_ = rhythmic_req->duration_.durlog_i_;
188             beam_p_->add_stem (stem_l);
189           }
190       }
191 }
192
193
194
195 ADD_THIS_TRANSLATOR(Beam_engraver);