]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release: 1.5.19
[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 TRANSLATOR_DECLARATIONS(Dynamic_engraver );
60   
61 protected:
62   virtual void finalize ();
63   virtual void acknowledge_grob (Grob_info);
64   virtual bool try_music (Music *req_l);
65   virtual void stop_translation_timestep ();
66   virtual void process_music ();  
67   virtual void start_translation_timestep ();
68 };
69
70
71
72
73 Dynamic_engraver::Dynamic_engraver ()
74 {
75   script_p_ = 0;
76   finished_cresc_p_ = 0;
77   line_spanner_ = 0;
78   finished_line_spanner_ = 0;
79   current_cresc_req_ = 0;
80   cresc_p_ =0;
81
82   script_req_l_ = 0;
83   accepted_spanreqs_drul_[START] = 0;
84   accepted_spanreqs_drul_[STOP] = 0;
85 }
86
87 void
88 Dynamic_engraver::start_translation_timestep ()
89 {
90   script_req_l_ = 0;
91   accepted_spanreqs_drul_[START] = 0;
92   accepted_spanreqs_drul_[STOP] = 0;
93 }
94
95 bool
96 Dynamic_engraver::try_music (Music * m)
97 {
98   if (dynamic_cast <Text_script_req*> (m)
99       && m->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
100     {
101       script_req_l_ = dynamic_cast<Text_script_req*> (m);
102       return true;
103     }
104   else if (Span_req* s =  dynamic_cast <Span_req*> (m))
105     {
106       String t = ly_scm2string (s->get_mus_property ("span-type"));
107       if (t== "abort")
108         {
109           accepted_spanreqs_drul_[LEFT] = 0;
110           accepted_spanreqs_drul_[RIGHT] = 0;
111           /*
112             Let's not kill the line spanner, since that would fuck up
113             earlier, not-to-be-terminated stuff.
114
115             It will disappear by itself when stop_translation_timestep
116  () finds that there is nothing to support anymore.  */
117           
118           if (cresc_p_)
119             cresc_p_->suicide ();
120           cresc_p_ = 0;
121         }
122       else if (t == "crescendo"
123            || t == "decrescendo")
124         {
125           accepted_spanreqs_drul_[s->get_span_dir ()] = s;
126           return true;
127         }
128     }
129   return false;
130 }
131
132 void
133 Dynamic_engraver::process_music ()
134 {
135   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
136     {
137       if (!line_spanner_)
138         {
139           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
140
141           Side_position_interface::set_axis (line_spanner_, Y_AXIS);
142           Axis_group_interface::set_interface (line_spanner_);
143           Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
144
145           Music * rq = accepted_spanreqs_drul_[START];
146           if (script_req_l_)
147             rq =  script_req_l_ ;
148           announce_grob (line_spanner_, rq);
149                          
150
151         }
152     }
153   
154         /*
155         During a (de)crescendo, pending request will not be cleared,
156         and a line-spanner will always be created, as \< \! are already
157         two requests.
158
159         Note: line-spanner must always have at least same duration
160         as (de)crecsendo, b.o. line-breaking.
161         */
162
163   
164
165   /*
166     maybe we should leave dynamic texts to the text-engraver and
167     simply acknowledge them?
168   */
169   if (script_req_l_)
170     {
171       script_p_ = new Item (get_property ("DynamicText"));
172       script_p_->set_grob_property ("text",
173                                    script_req_l_->get_mus_property ("text"));
174       
175       Side_position_interface::set_direction (script_p_, DOWN);
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       if (!cresc_p_)
193         {
194           accepted_spanreqs_drul_[STOP]->origin ()->warning
195  (_ ("can't find start of (de)crescendo"));
196           accepted_spanreqs_drul_[STOP] = 0;
197         }
198       else
199         {
200           assert (!finished_cresc_p_ && cresc_p_);
201
202           cresc_p_->set_bound (RIGHT, script_p_
203                                ? script_p_
204                                : unsmob_grob (get_property ("currentMusicalColumn")));
205           add_bound_item (line_spanner_, cresc_p_->get_bound (RIGHT));
206           
207
208           finished_cresc_p_ = cresc_p_;
209           cresc_p_ = 0;
210           current_cresc_req_ = 0;
211         }
212     }
213   
214   if (accepted_spanreqs_drul_[START])
215     {
216       if (current_cresc_req_)
217         {
218           accepted_spanreqs_drul_[START]->origin ()->warning
219  (current_cresc_req_->get_span_dir () == 1
220              ? _ ("already have a crescendo")
221              : _ ("already have a decrescendo"));
222         }
223       else
224         {
225           current_cresc_req_ = accepted_spanreqs_drul_[START];
226
227           /*
228             TODO: Use symbols.
229           */
230
231           String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
232
233           /*
234             ugh. Use push/pop?
235           */
236           SCM s = get_property ((start_type + "Spanner").ch_C ());
237           if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
238             {
239               cresc_p_  = new Spanner (get_property ("Hairpin"));
240               cresc_p_->set_grob_property ("grow-direction",
241                                            gh_int2scm ((start_type == "crescendo")
242                                                        ? BIGGER : SMALLER));
243               
244             }
245           /*
246             This is a convenient (and legacy) interface to TextSpanners
247             for use in (de)crescendi.
248             Hmm.
249           */
250           else
251             {
252               cresc_p_  = new Spanner (get_property ("TextSpanner"));
253               cresc_p_->set_interface (ly_symbol2scm ("dynamic-interface"));
254               cresc_p_->set_grob_property ("type", s);
255               
256               daddy_trans_l_->set_property ((start_type
257                                             + "Spanner").ch_C(), SCM_UNDEFINED);
258               s = get_property ((start_type + "Text").ch_C ());
259               /*
260                 FIXME: use markup_p () to check type.
261               */
262               if (gh_string_p (s) || gh_pair_p (s))
263                 {
264                   cresc_p_->set_grob_property ("edge-text",
265                                                gh_cons (s, ly_str02scm ("")));
266                   daddy_trans_l_->set_property ((start_type + "Text").ch_C(),
267                                                 SCM_UNDEFINED);
268                 }
269             }
270
271           cresc_p_->set_bound (LEFT, script_p_
272                                ? script_p_
273                                : unsmob_grob (get_property ("currentMusicalColumn")));
274
275           Axis_group_interface::add_element (line_spanner_, cresc_p_);
276
277           add_bound_item (line_spanner_, cresc_p_->get_bound (LEFT));
278           
279           announce_grob (cresc_p_, accepted_spanreqs_drul_[START]);
280         }
281     }
282 }
283
284 void
285 Dynamic_engraver::stop_translation_timestep ()
286 {
287   typeset_all ();
288   if (script_req_l_ && !current_cresc_req_)
289     {
290       finished_line_spanner_ = line_spanner_;
291       line_spanner_ =0;
292       typeset_all ();
293     }
294 }
295
296 void
297 Dynamic_engraver::finalize ()
298 {
299   typeset_all ();
300   
301   if (line_spanner_
302       && line_spanner_->immutable_property_alist_ == SCM_EOL)
303     line_spanner_ = 0;
304   if (line_spanner_)
305     {
306       finished_line_spanner_ = line_spanner_;
307       typeset_all ();
308     }
309
310   if (cresc_p_
311       && cresc_p_->immutable_property_alist_ == SCM_EOL)
312     cresc_p_ = 0;
313   if (cresc_p_)
314     {
315       current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
316       cresc_p_->suicide ();
317       cresc_p_ = 0;
318     }
319 }
320
321 void
322 Dynamic_engraver::typeset_all ()
323 {  
324   /*
325     remove suicided spanners,
326     ugh: we'll need this for every spanner, beam, slur
327     Hmm, how to do this, cleanly?
328     Maybe just check at typeset_grob ()?
329   */
330   if (finished_cresc_p_
331       && finished_cresc_p_->immutable_property_alist_ == SCM_EOL)
332     finished_cresc_p_ = 0;
333   if (finished_line_spanner_
334       && finished_line_spanner_->immutable_property_alist_ == SCM_EOL)
335     finished_line_spanner_ = 0;
336
337   if (finished_cresc_p_)
338     {
339       if (!finished_cresc_p_->get_bound (RIGHT))
340         {
341           finished_cresc_p_->set_bound (RIGHT, script_p_
342                                         ? script_p_
343                                         : unsmob_grob (get_property ("currentMusicalColumn")));
344
345           if (finished_line_spanner_)
346             add_bound_item (finished_line_spanner_,
347                             finished_cresc_p_->get_bound (RIGHT));
348         }
349       typeset_grob (finished_cresc_p_);
350       finished_cresc_p_ =0;
351     }
352   
353   if (script_p_)
354     {
355       typeset_grob (script_p_);
356       script_p_ = 0;
357     }
358   if (finished_line_spanner_)
359     {
360       /* To make sure that this works */
361       Side_position_interface::add_staff_support (finished_line_spanner_);
362       
363       /*
364         We used to have
365         
366              extend_spanner_over_elements (finished_line_spanner_);
367
368         but this is rather kludgy, since finished_line_spanner_
369         typically has a staff-symbol field set , extending it over the
370         entire staff.
371
372       */
373
374       Grob * l = finished_line_spanner_->get_bound (LEFT );
375       Grob * r = finished_line_spanner_->get_bound (RIGHT);      
376       if (!r && l)
377         finished_line_spanner_->set_bound (RIGHT, l);
378       else if (!l && r)
379         finished_line_spanner_->set_bound (LEFT, r);
380       else if (!r && !l)
381         {
382           /*
383             This is a isolated dynamic apparently, and does not even have
384             any interesting support item.
385            */
386           Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
387           Item * ci = dynamic_cast<Item*>(cc);
388           finished_line_spanner_->set_bound (RIGHT, ci);
389           finished_line_spanner_->set_bound (LEFT, ci);   
390         }
391         
392       typeset_grob (finished_line_spanner_);
393       finished_line_spanner_ = 0;
394     }
395 }
396
397 void
398 Dynamic_engraver::acknowledge_grob (Grob_info i)
399 {
400   if (Note_column::has_interface (i.grob_l_))
401     {
402       if (line_spanner_
403           /* Don't refill killed spanner */
404           && line_spanner_->immutable_property_alist_ != SCM_EOL)
405         {
406           Side_position_interface::add_support (line_spanner_,i.grob_l_);
407           add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_l_));
408         }
409     }
410 }
411 ENTER_DESCRIPTION(Dynamic_engraver,
412 /* descr */       "",
413 /* creats*/       "DynamicLineSpanner DynamicText Hairpin TextSpanner",
414 /* acks  */       "note-column-interface",
415 /* reads */       "",
416 /* write */       "");