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