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