]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
patch::: 1.3.129.jcn3
[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--2000 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           if (line_spanner_)
114             line_spanner_->suicide ();
115           line_spanner_ = 0;
116           if (cresc_p_)
117             cresc_p_->suicide ();
118           cresc_p_ = 0;
119         }
120       else if (t == "crescendo"
121            || t == "decrescendo")
122         {
123           accepted_spanreqs_drul_[s->get_span_dir()] = s;
124           return true;
125         }
126     }
127   return false;
128 }
129
130 void
131 Dynamic_engraver::process_music ()
132 {
133   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
134     {
135       if (!line_spanner_)
136         {
137           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
138
139           Side_position::set_axis (line_spanner_, Y_AXIS);
140           Axis_group_interface::set_interface (line_spanner_);
141           Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
142
143           Music * rq = accepted_spanreqs_drul_[START];
144           if (script_req_l_)
145             rq =  script_req_l_ ;
146           announce_grob (line_spanner_, rq);
147                          
148
149         }
150     }
151   
152         /*
153         During a (de)crescendo, pending request will not be cleared,
154         and a line-spanner will always be created, as \< \! are already
155         two requests.
156
157         Note: line-spanner must always have at least same duration
158         as (de)crecsendo, b.o. line-breaking.
159         */
160
161   
162
163   /*
164     maybe we should leave dynamic texts to the text-engraver and
165     simply acknowledge them?
166   */
167   if (script_req_l_)
168     {
169       script_p_ = new Item (get_property ("DynamicText"));
170       script_p_->set_grob_property ("text",
171                                    script_req_l_->get_mus_property ("text"));
172       
173       Side_position::set_direction (script_p_, LEFT);
174       Side_position::set_axis (script_p_, X_AXIS);
175       
176       if (Direction d = script_req_l_->get_direction ())
177         Directional_element_interface::set (line_spanner_, d);
178
179       Axis_group_interface::add_element (line_spanner_, script_p_);
180
181       announce_grob (script_p_, script_req_l_);
182     }
183
184   if (accepted_spanreqs_drul_[STOP])
185     {
186       /*
187         finish side position alignment if the (de)cresc ends here, and
188         there are no new dynamics.
189        */
190  
191       if ( !cresc_p_)
192         {
193           accepted_spanreqs_drul_[STOP]->origin ()->warning
194             (_ ("can't find start of (de)crescendo"));
195           accepted_spanreqs_drul_[STOP] = 0;
196         }
197       else
198         {
199           assert (!finished_cresc_p_ && cresc_p_);
200
201           cresc_p_->set_bound (RIGHT, script_p_
202                                ? script_p_
203                                : unsmob_grob (get_property ("currentMusicalColumn")));
204           add_bound_item (line_spanner_, cresc_p_->get_bound (RIGHT));
205           
206
207           finished_cresc_p_ = cresc_p_;
208           cresc_p_ = 0;
209           current_cresc_req_ = 0;
210         }
211     }
212   
213   if (accepted_spanreqs_drul_[START])
214     {
215       if (current_cresc_req_)
216         {
217           accepted_spanreqs_drul_[START]->origin ()->warning
218             (current_cresc_req_->get_span_dir() == 1
219              ? _ ("already have a crescendo")
220              : _ ("already have a decrescendo"));
221         }
222       else
223         {
224           current_cresc_req_ = accepted_spanreqs_drul_[START];
225
226           /*
227             TODO: Use symbols.
228           */
229
230           String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
231
232           /*
233             ugh. Use push/pop?
234           */
235           SCM s = get_property ((start_type + "Spanner").ch_C());
236           if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
237             {
238               cresc_p_  = new Spanner (get_property ("Hairpin"));
239               cresc_p_->set_grob_property ("grow-direction",
240                                            gh_int2scm ((start_type == "crescendo")
241                                                        ? BIGGER : SMALLER));
242               
243             }
244           /*
245             This is a convenient (and legacy) interface to TextSpanners
246             for use in (de)crescendi.
247             Hmm.
248           */
249           else
250             {
251               cresc_p_  = new Spanner (get_property ("TextSpanner"));
252               cresc_p_->set_grob_property ("type", s);
253               
254               daddy_trans_l_->set_property (start_type
255                                             + "Spanner", SCM_UNDEFINED);
256               s = get_property ((start_type + "Text").ch_C());
257               if (gh_string_p (s))
258                 {
259                   cresc_p_->set_grob_property ("edge-text",
260                                                gh_cons (s, ly_str02scm ("")));
261                   daddy_trans_l_->set_property (start_type + "Text",
262                                                 SCM_UNDEFINED);
263                 }
264             }
265
266           cresc_p_->set_bound (LEFT, script_p_
267                                ? script_p_
268                                : unsmob_grob (get_property ("currentMusicalColumn")));
269
270           Axis_group_interface::add_element (line_spanner_, cresc_p_);
271
272           add_bound_item (line_spanner_, cresc_p_->get_bound (LEFT));
273           
274           announce_grob (cresc_p_, accepted_spanreqs_drul_[START]);
275         }
276     }
277 }
278
279 void
280 Dynamic_engraver::stop_translation_timestep ()
281 {
282   typeset_all ();
283   if (script_req_l_ && !current_cresc_req_)
284     {
285       finished_line_spanner_ = line_spanner_;
286       line_spanner_ =0;
287       typeset_all ();
288     }
289 }
290
291 void
292 Dynamic_engraver::finalize ()
293 {
294   typeset_all ();
295   if (line_spanner_)
296     {
297       finished_line_spanner_ = line_spanner_;
298       typeset_all ();
299     }
300
301   if (cresc_p_)
302     {
303       current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
304       cresc_p_->suicide ();
305       cresc_p_ = 0;
306     }
307 }
308
309 void
310 Dynamic_engraver::typeset_all ()
311 {  
312   if (finished_cresc_p_)
313     {
314       if (!finished_cresc_p_->get_bound (RIGHT))
315         {
316           finished_cresc_p_->set_bound (RIGHT, script_p_
317                                         ? script_p_
318                                         : unsmob_grob (get_property ("currentMusicalColumn")));
319
320           if (finished_line_spanner_)
321             add_bound_item (finished_line_spanner_,
322                             finished_cresc_p_->get_bound (RIGHT));
323         }
324       typeset_grob (finished_cresc_p_);
325       finished_cresc_p_ =0;
326     }
327   
328   if (script_p_)
329     {
330       typeset_grob (script_p_);
331       script_p_ = 0;
332     }
333   if (finished_line_spanner_)
334     {
335       /*
336         To make sure that this works
337       */
338       Side_position::add_staff_support (finished_line_spanner_);
339       /*
340         We used to have
341         
342              extend_spanner_over_elements (finished_line_spanner_);
343
344         but this is rather kludgy, since finished_line_spanner_
345         typically has a staff-symbol field set , extending it over the
346         entire staff.
347
348       */
349
350       if (!finished_line_spanner_->get_bound (RIGHT))
351         finished_line_spanner_->set_bound (RIGHT, finished_line_spanner_->get_bound (LEFT));
352       
353       typeset_grob (finished_line_spanner_);
354       finished_line_spanner_ = 0;
355     }
356 }
357
358 void
359 Dynamic_engraver::acknowledge_grob (Grob_info i)
360 {
361   if (Note_column::has_interface (i.elem_l_))
362     {
363       if (line_spanner_)
364         {
365           Side_position::add_support (line_spanner_,i.elem_l_);
366           add_bound_item (line_spanner_,dynamic_cast<Item*>(i.elem_l_));
367         }
368     }
369 }