]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release: 1.3.31
[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--2000 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 #include "group-interface.hh"
22
23 /*
24   TODO:
25     * why handle absolute and span requests in one cryptic engraver,
26     what about Span_dynamic_engraver?
27
28     * hairpin
29     * text:
30       - `cresc. --  --  --'
31       - `cresc. poco a poco -- -- --'
32  */
33
34 /**
35    print text & hairpin dynamics.
36  */
37 class Dynamic_engraver : public Engraver
38 {
39   Text_item * abs_text_p_;
40   Text_item * cr_text_p_;
41   Crescendo * to_end_cresc_p_;
42   Crescendo * cresc_p_;
43
44   Span_req * cresc_req_l_;
45   Array<Request*> dynamic_req_l_arr_;
46   void  typeset_all ();
47 public:
48   VIRTUAL_COPY_CONS(Translator);
49   Dynamic_engraver();
50   
51 protected:
52
53   void announce_element (Score_element_info);
54   
55   virtual void do_removal_processing ();
56   virtual void acknowledge_element (Score_element_info);
57   virtual bool do_try_music (Music *req_l);
58   virtual void do_process_requests();
59   virtual void do_pre_move_processing();
60   virtual void do_post_move_processing();
61   virtual void typeset_element (Score_element*);
62 };
63
64 void
65 Dynamic_engraver::announce_element (Score_element_info i)
66 {
67   group (i.elem_l_, "interfaces").add_thing (ly_symbol2scm ("dynamic"));
68   
69   Engraver::announce_element (i);
70 }
71
72
73 Dynamic_engraver::Dynamic_engraver()
74 {
75   do_post_move_processing();
76   abs_text_p_ = 0;
77   cr_text_p_ = 0;
78   to_end_cresc_p_ = cresc_p_ = 0;
79   cresc_req_l_ = 0;
80 }
81
82 void
83 Dynamic_engraver::do_post_move_processing()
84 {
85   dynamic_req_l_arr_.clear();
86 }
87
88 bool
89 Dynamic_engraver::do_try_music (Music * m)
90 {
91   Request * r = dynamic_cast<Request*> (m);
92
93   if(Text_script_req * d = dynamic_cast <Text_script_req *> (r))
94     {
95       if (d->style_str_ != "dynamic")
96         return false;
97     }
98   else if (Span_req * s =  dynamic_cast <Span_req*> (r))
99     {
100       if (s-> span_type_str_ != "crescendo"
101           && s->span_type_str_ != "decrescendo")
102         return false;
103     }
104   else
105     return false;
106   
107   for (int i=0; i < dynamic_req_l_arr_.size (); i++)
108     if (r->equal_b (dynamic_req_l_arr_[i]))
109       return true;
110   
111   dynamic_req_l_arr_.push (r);
112   return true;
113 }
114
115
116 void
117 Dynamic_engraver::do_process_requests()
118 {
119   Crescendo*  new_cresc_p=0;
120
121   for (int i=0; i < dynamic_req_l_arr_.size(); i++)
122     {
123       if (Text_script_req *absd =
124           dynamic_cast<Text_script_req *> ( dynamic_req_l_arr_[i]))
125         {
126           if (abs_text_p_)
127             {
128               dynamic_req_l_arr_[i]->warning (_("Got a dynamic already.  Continuing dazed and confused."));
129               continue;
130             }
131           
132           String loud = absd->text_str_;
133
134           abs_text_p_ = new Text_item;
135           abs_text_p_->set_elt_property ("text",
136                                      ly_str02scm (loud.ch_C()));
137           abs_text_p_->set_elt_property ("style", gh_str02scm ("dynamic"));
138           abs_text_p_->set_elt_property ("script-priority",
139                                      gh_int2scm (100));
140           
141           Side_position_interface (abs_text_p_).set_axis (Y_AXIS);
142
143           
144           if (absd->get_direction ())
145             {
146               abs_text_p_->set_elt_property ("direction", gh_int2scm (absd->get_direction ()));
147             }
148
149
150           /*
151             UGH UGH 
152            */
153           SCM prop = get_property ("dynamicDirection");
154           if (!isdir_b (prop))
155             {
156               prop = get_property ("verticalDirection");
157             }
158
159           if (isdir_b (prop) && to_dir (prop))
160             abs_text_p_->set_elt_property ("direction", prop);
161
162           prop = get_property ("dynamicPadding");
163           if (gh_number_p(prop))
164             {
165               abs_text_p_->set_elt_property ("padding", prop);
166             }
167           announce_element (Score_element_info (abs_text_p_, absd));
168         }
169       else if (Span_req *span_l
170                = dynamic_cast <Span_req *> (dynamic_req_l_arr_[i]))
171         {
172           if (span_l->span_dir_ == STOP)
173             {
174               if (!cresc_p_)
175                 {
176                   span_l->warning (_ ("Can't find (de)crescendo to end"));
177                 }
178               else
179                 {
180                   assert (!to_end_cresc_p_);
181                   to_end_cresc_p_ =cresc_p_;
182                   
183                   cresc_p_ = 0;
184                 }
185             }
186           else if (span_l->span_dir_ == START)
187             {
188               cresc_req_l_ = span_l;
189               assert (!new_cresc_p);
190               new_cresc_p  = new Crescendo;
191               new_cresc_p->set_elt_property
192                 ("grow-direction",
193                  gh_int2scm ((span_l->span_type_str_ == "crescendo")
194                              ? BIGGER : SMALLER));
195               
196               SCM s = get_property (span_l->span_type_str_ + "Text");
197               if (gh_string_p (s))
198                 {
199                   cr_text_p_ = new Text_item;
200                   cr_text_p_->set_elt_property ("text", s);
201                   // urg
202                   cr_text_p_->set_elt_property ("style", gh_str02scm ("italic"));
203                   // ?
204                   cr_text_p_->set_elt_property ("script-priority",
205                                                 gh_int2scm (100));
206                   
207                   /*
208                     This doesn't work.
209                     I'd like to have support like this:
210                            |
211                           x|
212                           cresc. - - -
213
214                     or
215                            |
216                           x|
217                           ff cresc. - - -
218
219                    */
220                   if (0) //abs_text_p_)
221                     {
222                       Side_position_interface (cr_text_p_).set_axis (X_AXIS);
223                       Side_position_interface (cr_text_p_).add_support (abs_text_p_);
224                     }
225                   //Side_position_interface (cr_text_p_).set_axis (Y_AXIS);
226                   announce_element (Score_element_info (cr_text_p_, span_l));
227                 }
228
229               s = get_property (span_l->span_type_str_ + "Spanner");
230               if (gh_string_p (s)) //&& ly_scm2string (s) != "hairpin")
231                 {
232                   new_cresc_p->set_elt_property ("spanner", s);
233                 }
234           
235               side_position (new_cresc_p).set_axis (Y_AXIS);
236               announce_element (Score_element_info (new_cresc_p, span_l));
237             }
238         }
239     }
240
241   if (new_cresc_p)
242     {
243       if (cresc_p_)
244         {
245           ::warning (_ ("Too many crescendi here"));
246
247           typeset_element (cresc_p_);
248
249           cresc_p_ = 0;
250         }
251       
252       cresc_p_ = new_cresc_p;
253       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
254
255       // arrragh, brr, urg: we know how wide text is, no?
256       if (abs_text_p_)
257         {
258           index_set_cell (cresc_p_->get_elt_property ("dynamic-drul"),
259                           LEFT, SCM_BOOL_T);
260           if (to_end_cresc_p_)
261             index_set_cell (to_end_cresc_p_->get_elt_property ("dynamic-drul"),
262                             RIGHT, SCM_BOOL_T);
263         }
264     }
265 }
266
267 void
268 Dynamic_engraver::do_pre_move_processing()
269 {
270   typeset_all ();
271 }
272
273
274
275 ADD_THIS_TRANSLATOR(Dynamic_engraver);
276
277 void
278 Dynamic_engraver::do_removal_processing ()
279 {
280   if (cresc_p_)
281     {
282       typeset_element (cresc_p_ );
283       cresc_req_l_->warning (_ ("unended crescendo"));
284       cresc_p_ =0;
285     }
286   typeset_all ();
287 }
288
289
290 void
291 Dynamic_engraver::typeset_all ()
292 {  
293   if (to_end_cresc_p_)
294     {
295       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
296       typeset_element (to_end_cresc_p_);
297
298       to_end_cresc_p_ =0;
299
300     }
301   
302   if (abs_text_p_)
303     {
304       typeset_element (abs_text_p_);
305       abs_text_p_ = 0;
306     }
307
308   if (cr_text_p_)
309     {
310       typeset_element (cr_text_p_);
311       cr_text_p_ = 0;
312     }
313 }
314
315 void
316 Dynamic_engraver::typeset_element (Score_element * e)
317 {
318   side_position(e).add_staff_support ();
319   Engraver::typeset_element (e);
320 }
321
322 void
323 Dynamic_engraver::acknowledge_element (Score_element_info i)
324 {
325   if (dynamic_cast<Stem *> (i.elem_l_)
326       || dynamic_cast<Note_head *> (i.elem_l_)
327       )
328     {
329       if (abs_text_p_)
330         Side_position_interface (abs_text_p_).add_support (i.elem_l_);
331
332       if (cr_text_p_)  ///&& !abs_text_p_)
333         {
334           Side_position_interface (cr_text_p_).set_axis (Y_AXIS);
335           Side_position_interface (cr_text_p_).add_support (i.elem_l_);
336         }
337
338       if (to_end_cresc_p_)
339         Side_position_interface (to_end_cresc_p_).add_support (i.elem_l_);
340
341       if (cresc_p_)
342         Side_position_interface(cresc_p_).add_support (i.elem_l_);
343     }
344 }
345