]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
fe27ade94b64565db4b2e980b8bb54ac47f55900
[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 "paper-column.hh"
14 #include "staff-symbol.hh"
15 #include "note-column.hh"
16 #include "text-item.hh"
17 #include "side-position-interface.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_->set_elt_property ("text",
113                                      ly_str02scm (loud.ch_C()));
114           text_p_->set_elt_property ("style", gh_str02scm ("dynamic"));
115           text_p_->set_elt_property ("script-priority",
116                                      gh_int2scm (100));
117           text_p_->set_elt_property ("staff-support", SCM_BOOL_T);
118           
119           Side_position_interface (text_p_).set_axis (Y_AXIS);
120
121           
122           if (absd->get_direction ())
123             {
124               text_p_->set_elt_property ("direction", gh_int2scm (absd->get_direction ()));
125             }
126
127
128           /*
129             UGH UGH 
130            */
131           SCM prop = get_property ("dynamicDirection", 0);
132           if (!isdir_b (prop))
133             {
134               prop = get_property ("verticalDirection", 0);
135             }
136
137           if (isdir_b (prop) && to_dir (prop))
138             text_p_->set_elt_property ("direction", prop);
139
140           prop = get_property ("dynamicPadding", 0);
141           if (gh_number_p(prop))
142             {
143               text_p_->set_elt_property ("padding", prop);
144             }
145           announce_element (Score_element_info (text_p_, absd));
146         }
147       else if (Span_req *span_l
148                = dynamic_cast <Span_req *> (dynamic_req_l_arr_[i]))
149         {
150           if (span_l->span_dir_ == STOP)
151             {
152               if (!cresc_p_)
153                 {
154                   span_l->warning (_ ("Can't find (de)crescendo to end"));
155                 }
156               else
157                 {
158                   assert (!to_end_cresc_p_);
159                   to_end_cresc_p_ =cresc_p_;
160                   
161                   cresc_p_ = 0;
162                 }
163             }
164           else if (span_l->span_dir_ == START)
165             {
166               cresc_req_l_ = span_l;
167               assert (!new_cresc_p);
168               new_cresc_p  = new Crescendo;
169               new_cresc_p
170                 ->set_elt_property ("grow-direction",
171                                     gh_int2scm ((span_l->span_type_str_ == "crescendo") ? BIGGER : SMALLER));
172               
173               new_cresc_p->set_elt_property ("staff-support", SCM_BOOL_T);
174
175
176               Side_position_interface (new_cresc_p).set_axis (Y_AXIS);
177               announce_element (Score_element_info (new_cresc_p, span_l));
178             }
179         }
180     }
181
182   if (new_cresc_p)
183     {
184       if (cresc_p_)
185         {
186           ::warning (_ ("Too many crescendi here"));
187
188
189           typeset_element (cresc_p_);
190
191           cresc_p_ = 0;
192         }
193       
194       cresc_p_ = new_cresc_p;
195       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
196
197       if (text_p_)
198         {
199           index_set_cell (cresc_p_->get_elt_property ("dynamic-drul"),
200                           LEFT, SCM_BOOL_T);
201           if (to_end_cresc_p_)
202             index_set_cell (to_end_cresc_p_->get_elt_property ("dynamic-drul"),
203                             RIGHT, SCM_BOOL_T);
204         }
205     }
206 }
207
208 void
209 Dynamic_engraver::do_pre_move_processing()
210 {
211   typeset_all ();
212 }
213
214
215
216 ADD_THIS_TRANSLATOR(Dynamic_engraver);
217
218 void
219 Dynamic_engraver::do_removal_processing ()
220 {
221   if (cresc_p_)
222     {
223       typeset_element (cresc_p_ );
224       cresc_req_l_->warning (_ ("unended crescendo"));
225       cresc_p_ =0;
226     }
227   typeset_all ();
228 }
229
230
231 void
232 Dynamic_engraver::typeset_all ()
233 {  
234   if (to_end_cresc_p_)
235     {
236       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
237       typeset_element (to_end_cresc_p_);
238
239       to_end_cresc_p_ =0;
240
241     }
242   
243   if (text_p_)
244     {
245       typeset_element (text_p_);
246       text_p_ =0;
247     }
248 }
249
250
251 void
252 Dynamic_engraver::acknowledge_element (Score_element_info i)
253 {
254   if (dynamic_cast<Stem *> (i.elem_l_)
255       || dynamic_cast<Note_head *> (i.elem_l_)
256       )
257     {
258       if (text_p_)
259         Side_position_interface (text_p_).add_support (i.elem_l_);
260
261       if (to_end_cresc_p_)
262         Side_position_interface (to_end_cresc_p_).add_support (i.elem_l_);
263
264       if (cresc_p_)
265         Side_position_interface(cresc_p_).add_support (i.elem_l_);
266     }
267 }
268