]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "axis-group-interface.hh"
10 #include "context.hh"
11 #include "dimensions.hh"
12 #include "directional-element-interface.hh"
13 #include "engraver.hh"
14 #include "hairpin.hh"
15 #include "international.hh"
16 #include "interval.hh"
17 #include "note-column.hh"
18 #include "paper-column.hh"
19 #include "pointer-group-interface.hh"
20 #include "script-interface.hh"
21 #include "self-alignment-interface.hh"
22 #include "side-position-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "stream-event.hh"
25 #include "warn.hh"
26 #include "spanner.hh"
27
28 #include "translator.icc"
29
30 /*
31   TODO:
32
33   * direction of text-dynamic-event if not equal to direction of
34   line-spanner
35
36   - TODO: this engraver is too complicated. We should split it into
37   the handling of the basic grobs and the linespanner
38
39   - TODO: the line-spanner is not killed after the (de)crescs are
40   finished.
41 */
42
43 /**
44    print text & hairpin dynamics.
45 */
46 class Dynamic_engraver : public Engraver
47 {
48   Item *script_;
49   Spanner *line_spanner_;
50   Spanner *cresc_;
51
52   Spanner *finished_line_spanner_;
53   Spanner *finished_cresc_;
54
55   Stream_event *script_ev_;
56   Stream_event *current_cresc_ev_;
57
58   Drul_array<Stream_event *> accepted_spanevents_drul_;
59
60   vector<Note_column*> pending_columns_;
61   vector<Grob*> pending_elements_;
62
63   void typeset_all ();
64
65   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
66   DECLARE_ACKNOWLEDGER (accidental);
67   DECLARE_ACKNOWLEDGER (script);
68   DECLARE_ACKNOWLEDGER (stem_tremolo);
69   DECLARE_ACKNOWLEDGER (note_column);
70   DECLARE_ACKNOWLEDGER (slur);
71   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
72   DECLARE_TRANSLATOR_LISTENER (span_dynamic);
73
74 protected:
75   virtual void finalize ();
76   void stop_translation_timestep ();
77   void process_music ();
78 };
79
80 Dynamic_engraver::Dynamic_engraver ()
81 {
82   script_ = 0;
83   finished_cresc_ = 0;
84   line_spanner_ = 0;
85   finished_line_spanner_ = 0;
86   current_cresc_ev_ = 0;
87   cresc_ = 0;
88
89   script_ev_ = 0;
90   accepted_spanevents_drul_[START] = 0;
91   accepted_spanevents_drul_[STOP] = 0;
92 }
93
94 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
95 void
96 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
97 {
98   /*
99     TODO: probably broken.
100   */
101   ASSIGN_EVENT_ONCE (script_ev_, ev);
102 }
103
104 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
105 void
106 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
107 {
108   Direction d = to_dir (ev->get_property ("span-direction"));
109
110   if (d == START)
111     ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
112   
113   /* Cancel any ongoing crescendo, either explicitly by \! or
114      implicitly by a new crescendo. Also avoid warning if cresc is
115      cancelled both implicitly and explicitly. */
116   if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
117     ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
118 }
119
120 void
121 Dynamic_engraver::process_music ()
122 {
123   if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
124     {
125       if (!line_spanner_)
126         {
127           Stream_event *rq = accepted_spanevents_drul_[START];
128           line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
129           if (script_ev_)
130             rq = script_ev_;
131         }
132     }
133
134   /*
135     During a (de)crescendo, pending event will not be cleared,
136     and a line-spanner will always be created, as \< \! are already
137     two events.
138
139     Note: line-spanner must always have at least same duration
140     as (de)crecsendo, b.o. line-breaking.
141   */
142
143   /*
144     maybe we should leave dynamic texts to the text-engraver and
145     simply acknowledge them?
146   */
147   if (script_ev_)
148     {
149       script_ = make_item ("DynamicText", script_ev_->self_scm ());
150       script_->set_property ("text",
151                              script_ev_->get_property ("text"));
152
153       if (Direction d = to_dir (script_ev_->get_property ("direction")))
154         set_grob_direction (line_spanner_, d);
155       else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
156         set_grob_direction (script_, d);
157
158       Axis_group_interface::add_element (line_spanner_, script_);
159     }
160
161   Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
162     ? accepted_spanevents_drul_[STOP] : script_ev_;
163
164   if (accepted_spanevents_drul_[STOP] || script_ev_)
165     {
166       /*
167         finish side position alignment if the (de)cresc ends here, and
168         there are no new dynamics.
169       */
170
171       if (cresc_)
172         {
173           assert (!finished_cresc_ && cresc_);
174
175           if (script_)
176             {
177               cresc_->set_bound (RIGHT, script_);
178               add_bound_item (line_spanner_, script_);
179             }
180
181           finished_cresc_ = cresc_;
182           cresc_ = 0;
183           current_cresc_ev_ = 0;
184         }
185       else if (accepted_spanevents_drul_[STOP])
186         {
187           accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
188           stop_ev = 0;
189         }
190     }
191
192   if (accepted_spanevents_drul_[START])
193     {
194       if (current_cresc_ev_)
195         {
196           string msg = _ ("already have a decrescendo");
197           if (current_cresc_ev_->in_event_class ("crescendo-event"))
198             msg = _ ("already have a crescendo");
199
200           accepted_spanevents_drul_[START]->origin ()->warning (msg);
201           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
202         }
203       else
204         {
205           current_cresc_ev_ = accepted_spanevents_drul_[START];
206
207           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
208             set_grob_direction (line_spanner_, d);
209
210           /*
211             TODO: Use symbols.
212           */
213           
214           SCM start_sym = current_cresc_ev_->get_property ("class");
215           string start_type;
216           
217           if (start_sym == ly_symbol2scm ("decrescendo-event"))
218             start_type = "decrescendo";
219           else if (start_sym == ly_symbol2scm ("crescendo-event"))
220             start_type = "crescendo";
221           else
222             {
223               programming_error ("unknown dynamic spanner type");
224               return;
225             }
226
227           /*
228             UGH. TODO: should read from original event, so appearance
229             may be altered with \tweak.
230            */
231           SCM s = get_property ((start_type + "Spanner").c_str ());
232           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
233             {
234               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
235               if (finished_cresc_)
236                 {
237                   Pointer_group_interface::add_grob (finished_cresc_,
238                                                      ly_symbol2scm ("adjacent-hairpins"),
239                                                      cresc_);
240
241                   Pointer_group_interface::add_grob (cresc_,
242                                                      ly_symbol2scm ("adjacent-hairpins"),
243                                                      finished_cresc_);
244                 }
245             }
246
247           /*
248             This is a convenient (and legacy) interface to TextSpanners
249             for use in (de)crescendi.
250             Hmm.
251           */
252           else
253             {
254               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
255               cresc_->set_property ("style", s);
256               context ()->set_property ((start_type
257                                          + "Spanner").c_str (), SCM_EOL);
258               s = get_property ((start_type + "Text").c_str ());
259               /*
260                 FIXME: use get_markup () to check type.
261               */
262               if (scm_is_string (s) || scm_is_pair (s))
263                 {
264                   cresc_->set_property ("edge-text",
265                                         scm_cons (s, scm_makfrom0str ("")));
266                   context ()->set_property ((start_type + "Text").c_str (),
267                                             SCM_EOL);
268                 }
269             }
270
271           if (script_)
272             {
273               cresc_->set_bound (LEFT, script_);
274               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
275             }
276
277           Axis_group_interface::add_element (line_spanner_, cresc_);
278         }
279     }
280 }
281
282 void
283 Dynamic_engraver::stop_translation_timestep ()
284 {
285   if (!current_cresc_ev_ && line_spanner_)
286     {
287       assert (!finished_line_spanner_);
288       finished_line_spanner_ = line_spanner_;
289       line_spanner_ = 0;
290     }
291
292   typeset_all ();
293
294   if (cresc_ && !cresc_->get_bound (LEFT))
295     {
296       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
297       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
298     }
299
300   script_ev_ = 0;
301   accepted_spanevents_drul_[START] = 0;
302   accepted_spanevents_drul_[STOP] = 0;
303 }
304
305 void
306 Dynamic_engraver::finalize ()
307 {
308   typeset_all ();
309
310   if (line_spanner_
311       && !line_spanner_->is_live ())
312     line_spanner_ = 0;
313   if (line_spanner_)
314     {
315       finished_line_spanner_ = line_spanner_;
316       typeset_all ();
317     }
318
319   if (cresc_
320       && !cresc_->is_live ())
321     cresc_ = 0;
322   if (cresc_)
323     {
324       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
325       cresc_->suicide ();
326       cresc_ = 0;
327     }
328 }
329
330 void
331 Dynamic_engraver::typeset_all ()
332 {
333   if (finished_cresc_)
334     {
335       bool use_bar = to_boolean (get_property ("hairpinToBarline"))
336         && scm_is_string (get_property ("whichBar"))
337         && !script_ev_;
338                           
339       
340       if (!finished_cresc_->get_bound (RIGHT)
341           || use_bar)
342         {
343           Grob *column_bound = unsmob_grob (use_bar
344                                             ? get_property ("currentCommandColumn")
345                                             : get_property ("currentMusicalColumn"));
346             
347           finished_cresc_->set_bound (RIGHT, script_
348                                       ? script_
349                                       : column_bound);
350
351           if (finished_line_spanner_)
352             add_bound_item (finished_line_spanner_,
353                             finished_cresc_->get_bound (RIGHT));
354         }
355       finished_cresc_ = 0;
356     }
357
358   script_ = 0;
359   if (finished_line_spanner_)
360     {
361       /*
362         We used to have
363
364         extend-spanner-over-elements (finished_line_spanner_);
365
366         but this is rather kludgy, since finished_line_spanner_
367         typically has a staff-symbol field set , extending it over the
368         entire staff.
369
370       */
371
372       Grob *l = finished_line_spanner_->get_bound (LEFT);
373       Grob *r = finished_line_spanner_->get_bound (RIGHT);
374       if (!r && l)
375         finished_line_spanner_->set_bound (RIGHT, l);
376       else if (!l && r)
377         finished_line_spanner_->set_bound (LEFT, r);
378       else if (!r && !l)
379         {
380           /*
381             This is a isolated dynamic apparently, and does not even have
382             any interesting support item.
383           */
384           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
385           Item *ci = dynamic_cast<Item *> (cc);
386           finished_line_spanner_->set_bound (RIGHT, ci);
387           finished_line_spanner_->set_bound (LEFT, ci);
388         }
389
390       finished_line_spanner_ = 0;
391     }
392 }
393
394
395 void
396 Dynamic_engraver::acknowledge_accidental (Grob_info info)
397 {
398   if (line_spanner_)
399     Side_position_interface::add_support (line_spanner_, info.grob ());
400 }
401
402
403 void
404 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
405 {
406   if (line_spanner_)
407     Side_position_interface::add_support (line_spanner_, info.grob ());
408 }
409
410
411 void
412 Dynamic_engraver::acknowledge_slur (Grob_info info)
413 {
414   if (line_spanner_)
415     Side_position_interface::add_support (line_spanner_, info.grob ());
416 }
417
418
419 void
420 Dynamic_engraver::acknowledge_note_column (Grob_info info)
421 {
422   if (!line_spanner_)
423     return;
424
425   if (line_spanner_
426       /* Don't refill killed spanner */
427       && line_spanner_->is_live ())
428     {
429       Side_position_interface::add_support (line_spanner_, info.grob ());
430       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
431     }
432
433   if (script_ && !script_->get_parent (X_AXIS))
434     {
435       extract_grob_set (info.grob (), "note-heads", heads);
436       if (heads.size ())
437         {
438           Grob *head = heads[0];
439           script_->set_parent (head, X_AXIS);
440           Self_alignment_interface::set_center_parent (script_, X_AXIS);
441         }
442     }
443
444   if (cresc_)
445     {
446       if (!cresc_->get_bound (LEFT))
447         {
448           cresc_->set_bound (LEFT, info.grob ());
449           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
450         }
451     }
452
453   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
454     finished_cresc_->set_bound (RIGHT, info.grob ());
455 }
456
457 void
458 Dynamic_engraver::acknowledge_script (Grob_info info)
459 {
460   if (!line_spanner_ || !script_)
461     return;
462
463   SCM p = info.grob ()->get_property ("script-priority");
464
465   /*
466     UGH.
467
468     DynamicText doesn't really have a script-priority field.
469   */
470   if (scm_is_number (p)
471       && scm_to_int (p)
472       < scm_to_int (script_->get_property ("script-priority")))
473     Side_position_interface::add_support (line_spanner_, info.grob ());
474 }
475
476 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
477 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
478 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
479 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
480 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
481
482 ADD_TRANSLATOR (Dynamic_engraver,
483                 /* doc */
484                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
485                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
486                 "which takes care of vertical positioning.  ",
487
488                 /* create */
489                 "DynamicLineSpanner "
490                 "DynamicText "
491                 "Hairpin "
492                 "TextSpanner ",
493
494                 /* read */ "",
495                 /* write */ "");