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