]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
ef3576f881d273694f525a2472376ddd6b9f1b42
[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
69   virtual void create_grobs ();  
70   virtual void start_translation_timestep ();
71 };
72
73 ADD_THIS_TRANSLATOR (Dynamic_engraver);
74
75
76 Dynamic_engraver::Dynamic_engraver ()
77 {
78   script_p_ = 0;
79   finished_cresc_p_ = 0;
80   line_spanner_ = 0;
81   finished_line_spanner_ = 0;
82   current_cresc_req_ = 0;
83   cresc_p_ =0;
84
85   script_req_l_ = 0;
86   accepted_spanreqs_drul_[START] = 0;
87   accepted_spanreqs_drul_[STOP] = 0;
88 }
89
90 void
91 Dynamic_engraver::start_translation_timestep ()
92 {
93   script_req_l_ = 0;
94   accepted_spanreqs_drul_[START] = 0;
95   accepted_spanreqs_drul_[STOP] = 0;
96 }
97
98 bool
99 Dynamic_engraver::try_music (Music * m)
100 {
101   if (dynamic_cast <Text_script_req*> (m)
102       && m->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
103     {
104       script_req_l_ = dynamic_cast<Text_script_req*> (m);
105       return true;
106     }
107   else if (Span_req* s =  dynamic_cast <Span_req*> (m))
108     {
109       String t = ly_scm2string (s->get_mus_property ("span-type"));
110       if (t== "abort")
111         {
112           accepted_spanreqs_drul_[LEFT] = 0;
113           accepted_spanreqs_drul_[RIGHT] = 0;
114           if (line_spanner_)
115             line_spanner_->suicide ();
116           line_spanner_ = 0;
117           if (cresc_p_)
118             cresc_p_->suicide ();
119           cresc_p_ = 0;
120         }
121       else if (t == "crescendo"
122            || t == "decrescendo")
123         {
124           accepted_spanreqs_drul_[s->get_span_dir()] = s;
125           return true;
126         }
127     }
128   return false;
129 }
130
131 void
132 Dynamic_engraver::create_grobs ()
133 {
134   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
135     {
136       if (!line_spanner_)
137         {
138           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
139
140           Side_position::set_axis (line_spanner_, Y_AXIS);
141           Axis_group_interface::set_interface (line_spanner_);
142           Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
143
144           Music * rq = accepted_spanreqs_drul_[START];
145           if (script_req_l_)
146             rq =  script_req_l_ ;
147           announce_grob (line_spanner_, rq);
148                          
149
150         }
151     }
152   
153         /*
154         During a (de)crescendo, pending request will not be cleared,
155         and a line-spanner will always be created, as \< \! are already
156         two requests.
157
158         Note: line-spanner must always have at least same duration
159         as (de)crecsendo, b.o. line-breaking.
160         */
161
162   
163
164   /*
165     maybe we should leave dynamic texts to the text-engraver and
166     simply acknowledge them?
167   */
168   if (script_req_l_)
169     {
170       script_p_ = new Item (get_property ("DynamicText"));
171       script_p_->set_grob_property ("text",
172                                    script_req_l_->get_mus_property ("text"));
173       
174       Side_position::set_direction (script_p_, LEFT);
175       Side_position::set_axis (script_p_, X_AXIS);
176       
177       if (Direction d = script_req_l_->get_direction ())
178         Directional_element_interface::set (line_spanner_, d);
179
180       Axis_group_interface::add_element (line_spanner_, script_p_);
181
182       announce_grob (script_p_, script_req_l_);
183     }
184
185   if (accepted_spanreqs_drul_[STOP])
186     {
187       /*
188         finish side position alignment if the (de)cresc ends here, and
189         there are no new dynamics.
190     
191    */
192  
193       if ( !cresc_p_)
194         {
195           accepted_spanreqs_drul_[STOP]->origin ()->warning
196             (_ ("can't find start of (de)crescendo"));
197           accepted_spanreqs_drul_[STOP] = 0;
198         }
199       else
200         {
201           assert (!finished_cresc_p_ && cresc_p_);
202
203           cresc_p_->set_bound (RIGHT, script_p_
204                                ? script_p_
205                                : unsmob_grob (get_property ("currentMusicalColumn")));
206
207           finished_cresc_p_ = cresc_p_;
208           cresc_p_ = 0;
209           current_cresc_req_ = 0;
210         }
211     }
212   if (accepted_spanreqs_drul_[START])
213     {
214       if (current_cresc_req_)
215         {
216           accepted_spanreqs_drul_[START]->origin ()->warning
217             (current_cresc_req_->get_span_dir() == 1
218              ? _ ("already have a crescendo")
219              : _ ("already have a decrescendo"));
220         }
221       else
222         {
223           current_cresc_req_ = accepted_spanreqs_drul_[START];
224
225           /*
226             TODO: Use symbols.
227            */
228
229           String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
230
231           /*
232             ugh. Use push/pop?
233            */
234           SCM s = get_property ((start_type + "Spanner").ch_C());
235           if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
236             {
237               cresc_p_  = new Spanner (get_property ("Hairpin"));
238               cresc_p_->set_grob_property ("grow-direction",
239                                           gh_int2scm ((start_type == "crescendo")
240                                                       ? BIGGER : SMALLER));
241               
242             }
243           /*
244             This is a convenient (and legacy) interface to TextSpanners
245             for use in (de)crescendi.
246             Hmm.
247            */
248           else
249             {
250               cresc_p_  = new Spanner (get_property ("TextSpanner"));
251               cresc_p_->set_grob_property ("type", s);
252               
253               daddy_trans_l_->set_property (start_type
254                                             + "Spanner", SCM_UNDEFINED);
255               s = get_property ((start_type + "Text").ch_C());
256               if (gh_string_p (s))
257                 {
258                   cresc_p_->set_grob_property ("edge-text",
259                                               gh_cons (s, ly_str02scm ("")));
260                   daddy_trans_l_->set_property (start_type + "Text",
261                                                 SCM_UNDEFINED);
262                 }
263             }
264
265           cresc_p_->set_bound (LEFT, script_p_
266                                ? script_p_
267                                : unsmob_grob (get_property ("currentMusicalColumn")));
268
269           Axis_group_interface::add_element (line_spanner_, cresc_p_);
270           announce_grob (cresc_p_, accepted_spanreqs_drul_[START]);
271         }
272     }
273   script_req_l_ = 0;
274   accepted_spanreqs_drul_[START] = 0;
275   accepted_spanreqs_drul_[STOP] = 0;
276 }
277
278 void
279 Dynamic_engraver::stop_translation_timestep ()
280 {
281   typeset_all ();
282 }
283
284 void
285 Dynamic_engraver::finalize ()
286 {
287   typeset_all ();
288   if (line_spanner_)
289     {
290       finished_line_spanner_ = line_spanner_;
291       typeset_all ();
292     }
293
294   if (cresc_p_)
295     {
296       current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
297       cresc_p_->suicide ();
298       cresc_p_ = 0;
299     }
300 }
301
302 void
303 Dynamic_engraver::typeset_all ()
304 {  
305   if (finished_cresc_p_)
306     {
307 #if 1
308       finished_cresc_p_->set_bound (RIGHT, script_p_
309                            ? script_p_
310                            : unsmob_grob (get_property ("currentMusicalColumn")));
311 #endif          
312       typeset_grob (finished_cresc_p_);
313       finished_cresc_p_ =0;
314     }
315   
316   if (script_p_)
317     {
318       typeset_grob (script_p_);
319       script_p_ = 0;
320     }
321   if (finished_line_spanner_)
322     {
323       Side_position::add_staff_support (finished_line_spanner_);
324       extend_spanner_over_elements (finished_line_spanner_);
325       typeset_grob (finished_line_spanner_);
326       finished_line_spanner_ = 0;
327     }
328 }
329
330 void
331 Dynamic_engraver::acknowledge_grob (Grob_info i)
332 {
333   if (Note_column::has_interface (i.elem_l_))
334     {
335       if (line_spanner_)
336         {
337           Side_position::add_support (line_spanner_,i.elem_l_);
338           add_bound_item (line_spanner_,dynamic_cast<Item*>(i.elem_l_));
339         }
340     }
341 }