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