]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release: 1.3.132
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   dynamic-engraver.cc -- implement Dynamic_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "debug.hh"
9 #include "dimensions.hh"
10 #include "hairpin.hh"
11 #include "musical-request.hh"
12 #include "paper-column.hh"
13 #include "note-column.hh"
14 #include "item.hh"
15 #include "side-position-interface.hh"
16 #include "engraver.hh"
17 #include "group-interface.hh"
18 #include "directional-element-interface.hh"
19 #include "translator-group.hh"
20 #include "axis-group-interface.hh"
21
22
23 /*
24   TODO:
25
26   * direction of text-dynamic-request if not equal to direction of
27   line-spanner
28
29   - TODO: this engraver is too complicated. We should split it into
30   the handling of the basic grobs and the  linespanner
31
32   - TODO: the line-spanner is not killed after the (de)crescs are
33   finished.
34
35 */
36
37 /**
38    print text & hairpin dynamics.
39  */
40 class Dynamic_engraver : public Engraver
41 {
42   Item * script_p_;
43   Spanner * finished_cresc_p_;
44   Spanner * cresc_p_;
45
46   Text_script_req* script_req_l_;
47   
48   Span_req * current_cresc_req_;
49   Drul_array<Span_req*> accepted_spanreqs_drul_;
50
51   Spanner* line_spanner_;
52   Spanner* finished_line_spanner_;
53
54   Link_array<Note_column> pending_column_arr_;
55   Link_array<Grob> pending_element_arr_;
56   
57   void typeset_all ();
58
59 public:
60   VIRTUAL_COPY_CONS(Translator);
61   Dynamic_engraver ();
62   
63 protected:
64   virtual void finalize ();
65   virtual void acknowledge_grob (Grob_info);
66   virtual bool try_music (Music *req_l);
67   virtual void stop_translation_timestep ();
68   virtual void process_music ();  
69   virtual void start_translation_timestep ();
70 };
71
72 ADD_THIS_TRANSLATOR (Dynamic_engraver);
73
74
75 Dynamic_engraver::Dynamic_engraver ()
76 {
77   script_p_ = 0;
78   finished_cresc_p_ = 0;
79   line_spanner_ = 0;
80   finished_line_spanner_ = 0;
81   current_cresc_req_ = 0;
82   cresc_p_ =0;
83
84   script_req_l_ = 0;
85   accepted_spanreqs_drul_[START] = 0;
86   accepted_spanreqs_drul_[STOP] = 0;
87 }
88
89 void
90 Dynamic_engraver::start_translation_timestep ()
91 {
92   script_req_l_ = 0;
93   accepted_spanreqs_drul_[START] = 0;
94   accepted_spanreqs_drul_[STOP] = 0;
95 }
96
97 bool
98 Dynamic_engraver::try_music (Music * m)
99 {
100   if (dynamic_cast <Text_script_req*> (m)
101       && m->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
102     {
103       script_req_l_ = dynamic_cast<Text_script_req*> (m);
104       return true;
105     }
106   else if (Span_req* s =  dynamic_cast <Span_req*> (m))
107     {
108       String t = ly_scm2string (s->get_mus_property ("span-type"));
109       if (t== "abort")
110         {
111           accepted_spanreqs_drul_[LEFT] = 0;
112           accepted_spanreqs_drul_[RIGHT] = 0;
113           /*
114             Let's not kill the line spanner, since that would fuck up
115             earlier, not-to-be-terminated stuff.
116
117             It will disappear by itself when stop_translation_timestep
118             () finds that there is nothing to support anymore.  */
119           
120           if (cresc_p_)
121             cresc_p_->suicide ();
122           cresc_p_ = 0;
123         }
124       else if (t == "crescendo"
125            || t == "decrescendo")
126         {
127           accepted_spanreqs_drul_[s->get_span_dir()] = s;
128           return true;
129         }
130     }
131   return false;
132 }
133
134 void
135 Dynamic_engraver::process_music ()
136 {
137   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
138     {
139       if (!line_spanner_)
140         {
141           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
142
143           Side_position::set_axis (line_spanner_, Y_AXIS);
144           Axis_group_interface::set_interface (line_spanner_);
145           Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
146
147           Music * rq = accepted_spanreqs_drul_[START];
148           if (script_req_l_)
149             rq =  script_req_l_ ;
150           announce_grob (line_spanner_, rq);
151                          
152
153         }
154     }
155   
156         /*
157         During a (de)crescendo, pending request will not be cleared,
158         and a line-spanner will always be created, as \< \! are already
159         two requests.
160
161         Note: line-spanner must always have at least same duration
162         as (de)crecsendo, b.o. line-breaking.
163         */
164
165   
166
167   /*
168     maybe we should leave dynamic texts to the text-engraver and
169     simply acknowledge them?
170   */
171   if (script_req_l_)
172     {
173       script_p_ = new Item (get_property ("DynamicText"));
174       script_p_->set_grob_property ("text",
175                                    script_req_l_->get_mus_property ("text"));
176       
177       Side_position::set_direction (script_p_, LEFT);
178       Side_position::set_axis (script_p_, X_AXIS);
179       
180       if (Direction d = script_req_l_->get_direction ())
181         Directional_element_interface::set (line_spanner_, d);
182
183       Axis_group_interface::add_element (line_spanner_, script_p_);
184
185       announce_grob (script_p_, script_req_l_);
186     }
187
188   if (accepted_spanreqs_drul_[STOP])
189     {
190       /*
191         finish side position alignment if the (de)cresc ends here, and
192         there are no new dynamics.
193        */
194  
195       if ( !cresc_p_)
196         {
197           accepted_spanreqs_drul_[STOP]->origin ()->warning
198             (_ ("can't find start of (de)crescendo"));
199           accepted_spanreqs_drul_[STOP] = 0;
200         }
201       else
202         {
203           assert (!finished_cresc_p_ && cresc_p_);
204
205           cresc_p_->set_bound (RIGHT, script_p_
206                                ? script_p_
207                                : unsmob_grob (get_property ("currentMusicalColumn")));
208           add_bound_item (line_spanner_, cresc_p_->get_bound (RIGHT));
209           
210
211           finished_cresc_p_ = cresc_p_;
212           cresc_p_ = 0;
213           current_cresc_req_ = 0;
214         }
215     }
216   
217   if (accepted_spanreqs_drul_[START])
218     {
219       if (current_cresc_req_)
220         {
221           accepted_spanreqs_drul_[START]->origin ()->warning
222             (current_cresc_req_->get_span_dir() == 1
223              ? _ ("already have a crescendo")
224              : _ ("already have a decrescendo"));
225         }
226       else
227         {
228           current_cresc_req_ = accepted_spanreqs_drul_[START];
229
230           /*
231             TODO: Use symbols.
232           */
233
234           String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
235
236           /*
237             ugh. Use push/pop?
238           */
239           SCM s = get_property ((start_type + "Spanner").ch_C());
240           if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
241             {
242               cresc_p_  = new Spanner (get_property ("Hairpin"));
243               cresc_p_->set_grob_property ("grow-direction",
244                                            gh_int2scm ((start_type == "crescendo")
245                                                        ? BIGGER : SMALLER));
246               
247             }
248           /*
249             This is a convenient (and legacy) interface to TextSpanners
250             for use in (de)crescendi.
251             Hmm.
252           */
253           else
254             {
255               cresc_p_  = new Spanner (get_property ("TextSpanner"));
256               cresc_p_->set_grob_property ("type", s);
257               
258               daddy_trans_l_->set_property (start_type
259                                             + "Spanner", SCM_UNDEFINED);
260               s = get_property ((start_type + "Text").ch_C());
261               if (gh_string_p (s))
262                 {
263                   cresc_p_->set_grob_property ("edge-text",
264                                                gh_cons (s, ly_str02scm ("")));
265                   daddy_trans_l_->set_property (start_type + "Text",
266                                                 SCM_UNDEFINED);
267                 }
268             }
269
270           cresc_p_->set_bound (LEFT, script_p_
271                                ? script_p_
272                                : unsmob_grob (get_property ("currentMusicalColumn")));
273
274           Axis_group_interface::add_element (line_spanner_, cresc_p_);
275
276           add_bound_item (line_spanner_, cresc_p_->get_bound (LEFT));
277           
278           announce_grob (cresc_p_, accepted_spanreqs_drul_[START]);
279         }
280     }
281 }
282
283 void
284 Dynamic_engraver::stop_translation_timestep ()
285 {
286   typeset_all ();
287   if (script_req_l_ && !current_cresc_req_)
288     {
289       finished_line_spanner_ = line_spanner_;
290       line_spanner_ =0;
291       typeset_all ();
292     }
293 }
294
295 void
296 Dynamic_engraver::finalize ()
297 {
298   typeset_all ();
299   if (line_spanner_)
300     {
301       finished_line_spanner_ = line_spanner_;
302       typeset_all ();
303     }
304
305   if (cresc_p_)
306     {
307       current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
308       cresc_p_->suicide ();
309       cresc_p_ = 0;
310     }
311 }
312
313 void
314 Dynamic_engraver::typeset_all ()
315 {  
316   if (finished_cresc_p_)
317     {
318       if (!finished_cresc_p_->get_bound (RIGHT))
319         {
320           finished_cresc_p_->set_bound (RIGHT, script_p_
321                                         ? script_p_
322                                         : unsmob_grob (get_property ("currentMusicalColumn")));
323
324           if (finished_line_spanner_)
325             add_bound_item (finished_line_spanner_,
326                             finished_cresc_p_->get_bound (RIGHT));
327         }
328       typeset_grob (finished_cresc_p_);
329       finished_cresc_p_ =0;
330     }
331   
332   if (script_p_)
333     {
334       typeset_grob (script_p_);
335       script_p_ = 0;
336     }
337   if (finished_line_spanner_)
338     {
339       /*
340         To make sure that this works
341       */
342       Side_position::add_staff_support (finished_line_spanner_);
343       /*
344         We used to have
345         
346              extend_spanner_over_elements (finished_line_spanner_);
347
348         but this is rather kludgy, since finished_line_spanner_
349         typically has a staff-symbol field set , extending it over the
350         entire staff.
351
352       */
353
354       if (!finished_line_spanner_->get_bound (RIGHT))
355         finished_line_spanner_->set_bound (RIGHT, finished_line_spanner_->get_bound (LEFT));
356       
357       typeset_grob (finished_line_spanner_);
358       finished_line_spanner_ = 0;
359     }
360 }
361
362 void
363 Dynamic_engraver::acknowledge_grob (Grob_info i)
364 {
365   if (Note_column::has_interface (i.elem_l_))
366     {
367       if (line_spanner_)
368         {
369           Side_position::add_support (line_spanner_,i.elem_l_);
370           add_bound_item (line_spanner_,dynamic_cast<Item*>(i.elem_l_));
371         }
372     }
373 }