]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release: 1.3.8
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   dynamic-reg.cc -- implement Dynamic_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "debug.hh"
9 #include "crescendo.hh"
10 #include "musical-request.hh"
11 #include "lookup.hh"
12 #include "paper-def.hh"
13 #include "score-column.hh"
14 #include "staff-symbol.hh"
15 #include "note-column.hh"
16 #include "text-item.hh"
17 #include "staff-side.hh"
18 #include "engraver.hh"
19 #include "stem.hh"
20 #include "note-head.hh"
21
22 /**
23    print text & hairpin dynamics.
24  */
25 class Dynamic_engraver : public Engraver
26 {
27   Text_item * text_p_;
28   Crescendo * to_end_cresc_p_;
29   Crescendo * cresc_p_;
30
31   Span_req * cresc_req_l_;
32   Array<Request*> dynamic_req_l_arr_;
33   void  typeset_all ();
34 public:
35   VIRTUAL_COPY_CONS(Translator);
36   Dynamic_engraver();
37   
38 protected:
39   virtual void do_removal_processing ();
40   virtual void acknowledge_element (Score_element_info);
41   virtual bool do_try_music (Music *req_l);
42   virtual void do_process_requests();
43   virtual void do_pre_move_processing();
44   virtual void do_post_move_processing();
45 };
46
47
48
49 Dynamic_engraver::Dynamic_engraver()
50 {
51   do_post_move_processing();
52   text_p_ =0;
53
54   to_end_cresc_p_ = cresc_p_ = 0;
55
56   cresc_req_l_ = 0;
57 }
58
59 void
60 Dynamic_engraver::do_post_move_processing()
61 {
62   dynamic_req_l_arr_.clear();
63 }
64
65 bool
66 Dynamic_engraver::do_try_music (Music * m)
67 {
68   Request * r = dynamic_cast<Request*> (m);
69
70   if(Text_script_req * d = dynamic_cast <Text_script_req *> (r))
71     {
72       if (d->style_str_ != "dynamic")
73         return false;
74     }
75   else if (Span_req * s =  dynamic_cast <Span_req*> (r))
76     {
77       if (s-> span_type_str_ != "crescendo"
78           && s->span_type_str_ != "decrescendo")
79         return false;
80     }
81   else
82     return false;
83   
84   for (int i=0; i < dynamic_req_l_arr_.size (); i++)
85     if (r->equal_b (dynamic_req_l_arr_[i]))
86       return true;
87   
88   dynamic_req_l_arr_.push (r);
89   return true;
90 }
91
92
93 void
94 Dynamic_engraver::do_process_requests()
95 {
96   Crescendo*  new_cresc_p=0;
97
98   for (int i=0; i < dynamic_req_l_arr_.size(); i++)
99     {
100       if (Text_script_req *absd =
101           dynamic_cast<Text_script_req *> ( dynamic_req_l_arr_[i]))
102         {
103           if (text_p_)
104             {
105               dynamic_req_l_arr_[i]->warning (_("Got a dynamic already.  Continuing dazed and confused."));
106               continue;
107             }
108           
109           String loud = absd->text_str_;
110
111           text_p_ = new Text_item;
112           text_p_->text_str_ =  loud; // ugh
113           text_p_->set_elt_property ("style", gh_str02scm ("dynamic"));
114           text_p_->set_elt_property ("script-priority",
115                                      gh_int2scm (100));
116           Staff_sidify (text_p_).set_axis (Y_AXIS);
117
118           
119           if (absd->get_direction ())
120             {
121               text_p_->set_elt_property ("direction", gh_int2scm (absd->get_direction ()));
122             }
123
124
125           /*
126             UGH UGH 
127            */
128           SCM prop = get_property ("dynamicDirection", 0);
129           if (!isdir_b (prop))
130             {
131               prop = get_property ("verticalDirection", 0);
132             }
133
134           if (isdir_b (prop) && to_dir (prop))
135             text_p_->set_elt_property ("direction", prop);
136
137
138           prop = get_property ("dynamicPadding", 0);
139           if (gh_number_p(prop))
140             {
141               text_p_->set_elt_property ("padding", prop);
142             }
143           announce_element (Score_element_info (text_p_, absd));
144         }
145       else if (Span_req *span_l
146                = dynamic_cast <Span_req *> (dynamic_req_l_arr_[i]))
147         {
148           if (span_l->span_dir_ == STOP)
149             {
150               if (!cresc_p_)
151                 {
152                   span_l->warning (_ ("Can't find (de)crescendo to end"));
153                 }
154               else
155                 {
156                   assert (!to_end_cresc_p_);
157                   to_end_cresc_p_ =cresc_p_;
158                   
159                   cresc_p_ = 0;
160                 }
161             }
162           else if (span_l->span_dir_ == START)
163             {
164               cresc_req_l_ = span_l;
165               assert (!new_cresc_p);
166               new_cresc_p  = new Crescendo;
167               new_cresc_p->grow_dir_ = (span_l->span_type_str_ == "crescendo")  ? BIGGER : SMALLER;
168
169               Staff_sidify (new_cresc_p).set_axis (Y_AXIS);
170               announce_element (Score_element_info (new_cresc_p, span_l));
171             }
172         }
173     }
174
175   if (new_cresc_p)
176     {
177       if (cresc_p_)
178         {
179           ::warning (_ ("Too many crescendi here"));
180           typeset_element (cresc_p_);
181
182           cresc_p_ = 0;
183         }
184       
185       cresc_p_ = new_cresc_p;
186       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
187
188       if (text_p_)
189         {
190           cresc_p_->dyn_b_drul_[LEFT] = true;
191           if (to_end_cresc_p_)
192             to_end_cresc_p_->dyn_b_drul_[RIGHT] = true;
193         }
194     }
195 }
196
197 void
198 Dynamic_engraver::do_pre_move_processing()
199 {
200   typeset_all ();
201 }
202
203
204
205 ADD_THIS_TRANSLATOR(Dynamic_engraver);
206
207 void
208 Dynamic_engraver::do_removal_processing ()
209 {
210   if (cresc_p_)
211     {
212       typeset_element (cresc_p_ );
213       cresc_req_l_->warning (_ ("unended crescendo"));
214       cresc_p_ =0;
215     }
216   typeset_all ();
217 }
218
219
220 void
221 Dynamic_engraver::typeset_all ()
222 {  
223   if (to_end_cresc_p_)
224     {
225       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
226
227       typeset_element (to_end_cresc_p_);
228
229       to_end_cresc_p_ =0;
230
231     }
232   
233   if (text_p_)
234     {
235       typeset_element (text_p_);
236       text_p_ =0;
237     }
238 }
239
240
241 void
242 Dynamic_engraver::acknowledge_element (Score_element_info i)
243 {
244   if (dynamic_cast<Stem *> (i.elem_l_)
245       || dynamic_cast<Note_head *> (i.elem_l_)
246       )
247     {
248       if (text_p_)
249         Staff_sidify (text_p_).add_support (i.elem_l_);
250
251       if (to_end_cresc_p_)
252         Staff_sidify (to_end_cresc_p_).add_support (i.elem_l_);
253
254       if (cresc_p_)
255         Staff_sidify(cresc_p_).add_support (i.elem_l_);
256     }
257 }
258