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