]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
* input/regression/hairpin-dashed.ly: new file.
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "warn.hh"
9 #include "dimensions.hh"
10 #include "hairpin.hh"
11 #include "event.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 #include "script.hh"
22
23 /*
24   TODO:
25
26   * direction of text-dynamic-event 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_;
43   Spanner * finished_cresc_;
44   Spanner * cresc_;
45
46   Music* script_ev_;
47   
48   Music * current_cresc_ev_;
49   Drul_array<Music*> accepted_spanreqs_drul_;
50
51   Spanner* line_spanner_;
52   Spanner* finished_line_spanner_;
53
54   Link_array<Note_column> pending_columns_;
55   Link_array<Grob> pending_elements_;
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);
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_ = 0;
76   finished_cresc_ = 0;
77   line_spanner_ = 0;
78   finished_line_spanner_ = 0;
79   current_cresc_ev_ = 0;
80   cresc_ =0;
81
82   script_ev_ = 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_ev_ = 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 (m->is_mus_type ("absolute-dynamic-event"))
99     {
100       /*
101         TODO: probably broken.
102       */
103       script_ev_ = m;
104       return true;
105     }
106   else if (m->is_mus_type ("abort-event"))
107     {
108       accepted_spanreqs_drul_[LEFT] = 0;
109       accepted_spanreqs_drul_[RIGHT] = 0;
110       /*
111         Let's not kill the line spanner, since that would fuck up
112         earlier, not-to-be-terminated stuff.
113
114         It will disappear by itself when stop_translation_timestep
115         () finds that there is nothing to support anymore.  */
116           
117       if (cresc_)
118         cresc_->suicide ();
119       cresc_ = 0;
120     }
121   else if (m->is_mus_type ("decrescendo-event")
122            || m->is_mus_type ("crescendo-event"))
123     {
124       Direction d = to_dir (m->get_mus_property ("span-direction"));
125
126       accepted_spanreqs_drul_[d] = m;
127       if (current_cresc_ev_ && d == START)
128         accepted_spanreqs_drul_[STOP] = m;
129       return true;
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_ev_)
138     {
139       if (!line_spanner_)
140         {
141           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
142
143           Music * rq = accepted_spanreqs_drul_[START];
144           if (script_ev_)
145             rq =  script_ev_ ;
146           announce_grob(line_spanner_, rq ? rq->self_scm(): SCM_EOL);
147         }
148     }
149   
150   /*
151     During a (de)crescendo, pending event will not be cleared,
152     and a line-spanner will always be created, as \< \! are already
153     two events.
154
155     Note: line-spanner must always have at least same duration
156     as (de)crecsendo, b.o. line-breaking.
157   */
158
159   
160
161   /*
162     maybe we should leave dynamic texts to the text-engraver and
163     simply acknowledge them?
164   */
165   if (script_ev_)
166     {
167       script_ = new Item (get_property ("DynamicText"));
168       script_->set_grob_property ("text",
169                                    script_ev_->get_mus_property ("text"));
170
171       
172       if (Direction d = to_dir (script_ev_->get_mus_property ("direction")))
173         Directional_element_interface::set (line_spanner_, d);
174
175       Axis_group_interface::add_element (line_spanner_, script_);
176
177       announce_grob(script_, script_ev_->self_scm());
178     }
179
180   Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
181     accepted_spanreqs_drul_[STOP] : script_ev_;
182
183   if (accepted_spanreqs_drul_[STOP] || script_ev_)
184     {
185       /*
186         finish side position alignment if the (de)cresc ends here, and
187         there are no new dynamics.
188        */
189
190
191       if (cresc_)
192         {
193           assert (!finished_cresc_ && cresc_);
194
195           cresc_->set_bound (RIGHT, script_
196                                ? script_
197                                : unsmob_grob (get_property ("currentMusicalColumn")));
198           add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
199           
200
201           finished_cresc_ = cresc_;
202           cresc_ = 0;
203           current_cresc_ev_ = 0;
204         }
205       else if (accepted_spanreqs_drul_[STOP] )
206         {
207           accepted_spanreqs_drul_[STOP]->origin ()->warning(_ ("can't find start of (de)crescendo"));
208           stop_ev = 0;
209         }
210       
211     }
212   
213   if (accepted_spanreqs_drul_[START])
214     {
215       if (current_cresc_ev_)
216         {
217           Direction sd = to_dir (current_cresc_ev_->get_mus_property ("span-direction"));
218           String msg = sd == 1
219             ? _ ("already have a crescendo")
220             : _ ("already have a decrescendo");
221       
222           accepted_spanreqs_drul_[START]->origin ()->warning (msg);
223           current_cresc_ev_->origin ()->warning (_("Cresc started here"));
224         }
225       else
226         {
227           current_cresc_ev_ = accepted_spanreqs_drul_[START];
228
229           if (Direction d = to_dir (current_cresc_ev_->get_mus_property ("direction")))
230             Directional_element_interface::set (line_spanner_, d);
231
232           /*
233             TODO: Use symbols.
234           */
235
236           String start_type = 
237             ly_symbol2string (current_cresc_ev_->get_mus_property ("name"));
238
239           /*
240             ugh. Use push/pop?
241           */
242           if (start_type == "DecrescendoEvent")
243             start_type = "decrescendo";
244           else if (start_type == "CrescendoEvent")
245             start_type = "crescendo";
246           
247           SCM s = get_property ((start_type + "Spanner").to_str0 ());
248           if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
249             {
250               cresc_  = new Spanner (get_property ("Hairpin"));
251               cresc_->set_grob_property ("grow-direction",
252                                            gh_int2scm ((start_type == "crescendo")
253                                                        ? BIGGER : SMALLER));
254               
255             }
256
257           
258           /*
259             This is a convenient (and legacy) interface to TextSpanners
260             for use in (de)crescendi.
261             Hmm.
262           */
263           else
264             {
265               cresc_  = new Spanner (get_property ("TextSpanner"));
266               cresc_->set_grob_property ("style", s);
267               daddy_trans_->set_property ((start_type
268                                             + "Spanner").to_str0 (), SCM_EOL);
269               s = get_property ((start_type + "Text").to_str0 ());
270               /*
271                 FIXME: use get_markup () to check type.
272               */
273               if (gh_string_p (s) || gh_pair_p (s))
274                 {
275                   cresc_->set_grob_property ("edge-text",
276                                              gh_cons (s, scm_makfrom0str ("")));
277                   daddy_trans_->set_property ((start_type + "Text").to_str0 (),
278                                                 SCM_EOL);
279                 }
280             }
281
282           cresc_->set_bound (LEFT, script_
283                                ? script_
284                                : unsmob_grob (get_property ("currentMusicalColumn")));
285
286           Axis_group_interface::add_element (line_spanner_, cresc_);
287
288           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
289           
290           announce_grob(cresc_, accepted_spanreqs_drul_[START]->self_scm());
291         }
292     }
293 }
294
295 void
296 Dynamic_engraver::stop_translation_timestep ()
297 {
298   typeset_all ();
299   if (!current_cresc_ev_)
300     {
301       finished_line_spanner_ = line_spanner_;
302       line_spanner_ =0;
303       typeset_all ();
304     }
305 }
306
307 void
308 Dynamic_engraver::finalize ()
309 {
310   typeset_all ();
311   
312   if (line_spanner_
313       && !line_spanner_->live())
314     line_spanner_ = 0;
315   if (line_spanner_)
316     {
317       finished_line_spanner_ = line_spanner_;
318       typeset_all ();
319     }
320
321   if (cresc_
322       && !cresc_->live())
323     cresc_ = 0;
324   if (cresc_)
325     {
326       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
327       cresc_->suicide ();
328       cresc_ = 0;
329     }
330 }
331
332 void
333 Dynamic_engraver::typeset_all ()
334 {  
335   /*
336     remove suicided spanners,
337     ugh: we'll need this for every spanner, beam, slur
338     Hmm, how to do this, cleanly?
339     Maybe just check at typeset_grob ()?
340   */
341   if (finished_cresc_
342       && !finished_cresc_->live())
343     finished_cresc_ = 0;
344   if (finished_line_spanner_
345       && !finished_line_spanner_->live())
346     finished_line_spanner_ = 0;
347
348   if (finished_cresc_)
349     {
350       if (!finished_cresc_->get_bound (RIGHT))
351         {
352           finished_cresc_->set_bound (RIGHT, script_
353                                         ? script_
354                                         : unsmob_grob (get_property ("currentMusicalColumn")));
355
356           if (finished_line_spanner_)
357             add_bound_item (finished_line_spanner_,
358                             finished_cresc_->get_bound (RIGHT));
359         }
360       typeset_grob (finished_cresc_);
361       finished_cresc_ =0;
362     }
363   
364   if (script_)
365     {
366       typeset_grob (script_);
367       script_ = 0;
368     }
369   if (finished_line_spanner_)
370     {
371       /* To make sure that this works */
372       Side_position_interface::add_staff_support (finished_line_spanner_);
373       
374       /*
375         We used to have
376         
377              extend-spanner-over-elements (finished_line_spanner_);
378
379         but this is rather kludgy, since finished_line_spanner_
380         typically has a staff-symbol field set , extending it over the
381         entire staff.
382
383       */
384
385       Grob * l = finished_line_spanner_->get_bound (LEFT );
386       Grob * r = finished_line_spanner_->get_bound (RIGHT);      
387       if (!r && l)
388         finished_line_spanner_->set_bound (RIGHT, l);
389       else if (!l && r)
390         finished_line_spanner_->set_bound (LEFT, r);
391       else if (!r && !l)
392         {
393           /*
394             This is a isolated dynamic apparently, and does not even have
395             any interesting support item.
396            */
397           Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
398           Item * ci = dynamic_cast<Item*>(cc);
399           finished_line_spanner_->set_bound (RIGHT, ci);
400           finished_line_spanner_->set_bound (LEFT, ci);   
401         }
402         
403       typeset_grob (finished_line_spanner_);
404       finished_line_spanner_ = 0;
405     }
406 }
407
408 void
409 Dynamic_engraver::acknowledge_grob (Grob_info i)
410 {
411   if (!line_spanner_)
412     return ;
413   
414   if (Note_column::has_interface (i.grob_))
415     {
416       if (line_spanner_
417           /* Don't refill killed spanner */
418           && line_spanner_->live())
419         {
420           Side_position_interface::add_support (line_spanner_,i.grob_);
421           add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_));
422         }
423
424       if (script_ && !script_->get_parent (X_AXIS))
425         {
426           script_->set_parent (i.grob_,  X_AXIS);
427         }
428       
429     }
430   else if (Script_interface::has_interface (i.grob_) && script_)
431     {
432       SCM p = i.grob_->get_grob_property ("script-priority");
433
434       /*
435         UGH.
436
437         DynamicText doesn't really have a script-priority field.
438        */
439       if (gh_number_p (p)
440           && gh_scm2int (p) < gh_scm2int (script_->get_grob_property ("script-priority")))
441         {
442           Side_position_interface::add_support (line_spanner_, i.grob_);
443
444         }         
445     }
446 }
447 ENTER_DESCRIPTION(Dynamic_engraver,
448 /* descr */       
449 "This engraver creates hairpins, dynamic texts, and their vertical\n"
450 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
451 "which takes care of vertical positioning.  "
452 ,
453                   
454 /* creats*/       "DynamicLineSpanner DynamicText Hairpin TextSpanner",
455 /* accepts */     "absolute-dynamic-event crescendo-event decrescendo-event",
456 /* acks  */      "note-column-interface script-interface",
457 /* reads */       "",
458 /* write */       "");