]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
fix tuplet example
[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--2007 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 #include "text-interface.hh"
28
29 #include "translator.icc"
30
31 /*
32   TODO:
33
34   * direction of text-dynamic-event if not equal to direction of
35   line-spanner
36
37   - TODO: this engraver is too complicated. We should split it into
38   the handling of the basic grobs and the linespanner
39
40   - TODO: the line-spanner is not killed after the (de)crescs are
41   finished.
42 */
43
44 /**
45    print text & hairpin dynamics.
46 */
47 class Dynamic_engraver : public Engraver
48 {
49   Item *script_;
50   Spanner *line_spanner_;
51   Spanner *cresc_;
52
53   Spanner *finished_line_spanner_;
54   Spanner *finished_cresc_;
55
56   Stream_event *script_ev_;
57   Stream_event *current_cresc_ev_;
58
59   Drul_array<Stream_event *> accepted_spanevents_drul_;
60
61   vector<Note_column*> pending_columns_;
62   vector<Grob*> pending_elements_;
63
64   void typeset_all ();
65
66   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
67   DECLARE_ACKNOWLEDGER (accidental);
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 (_ ("cannot 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               if (Text_interface::is_markup (s))
260                 {
261                   cresc_->set_property ("text", s);
262                   context ()->set_property ((start_type + "Text").c_str (),
263                                             SCM_EOL);
264                 }
265             }
266
267           if (script_)
268             {
269               cresc_->set_bound (LEFT, script_);
270               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
271             }
272
273           Axis_group_interface::add_element (line_spanner_, cresc_);
274         }
275     }
276 }
277
278 void
279 Dynamic_engraver::stop_translation_timestep ()
280 {
281   if (!current_cresc_ev_ && line_spanner_)
282     {
283       assert (!finished_line_spanner_);
284       finished_line_spanner_ = line_spanner_;
285       line_spanner_ = 0;
286     }
287
288   typeset_all ();
289
290   if (cresc_ && !cresc_->get_bound (LEFT))
291     {
292       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
293       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
294     }
295
296   script_ev_ = 0;
297   accepted_spanevents_drul_[START] = 0;
298   accepted_spanevents_drul_[STOP] = 0;
299 }
300
301 void
302 Dynamic_engraver::finalize ()
303 {
304   typeset_all ();
305
306   if (line_spanner_
307       && !line_spanner_->is_live ())
308     line_spanner_ = 0;
309   if (line_spanner_)
310     {
311       finished_line_spanner_ = line_spanner_;
312       typeset_all ();
313     }
314
315   if (cresc_
316       && !cresc_->is_live ())
317     cresc_ = 0;
318   if (cresc_)
319     {
320       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
321       cresc_->suicide ();
322       cresc_ = 0;
323     }
324 }
325
326 void
327 Dynamic_engraver::typeset_all ()
328 {
329   if (finished_cresc_)
330     {
331       bool use_bar = to_boolean (get_property ("hairpinToBarline"))
332         && scm_is_string (get_property ("whichBar"))
333         && !script_ev_;
334                           
335       
336       if (!finished_cresc_->get_bound (RIGHT)
337           || use_bar)
338         {
339                   
340           Grob *column_bound = 0;
341           if (use_bar)
342             {
343               column_bound = unsmob_grob (get_property ("breakableSeparationItem"));
344             }
345           
346           if (!column_bound)
347             column_bound = unsmob_grob (use_bar
348                                         ? get_property ("currentCommandColumn")
349                                         : get_property ("currentMusicalColumn"));
350           
351           finished_cresc_->set_bound (RIGHT, script_
352                                       ? script_
353                                       : column_bound);
354
355           if (finished_line_spanner_)
356             add_bound_item (finished_line_spanner_,
357                             finished_cresc_->get_bound (RIGHT));
358         }
359       finished_cresc_ = 0;
360     }
361
362   script_ = 0;
363   if (finished_line_spanner_)
364     {
365       /*
366         We used to have
367
368         extend-spanner-over-elements (finished_line_spanner_);
369
370         but this is rather kludgy, since finished_line_spanner_
371         typically has a staff-symbol field set , extending it over the
372         entire staff.
373
374       */
375
376       Grob *l = finished_line_spanner_->get_bound (LEFT);
377       Grob *r = finished_line_spanner_->get_bound (RIGHT);
378       if (!r && l)
379         finished_line_spanner_->set_bound (RIGHT, l);
380       else if (!l && r)
381         finished_line_spanner_->set_bound (LEFT, r);
382       else if (!r && !l)
383         {
384           /*
385             This is a isolated dynamic apparently, and does not even have
386             any interesting support item.
387           */
388           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
389           Item *ci = dynamic_cast<Item *> (cc);
390           finished_line_spanner_->set_bound (RIGHT, ci);
391           finished_line_spanner_->set_bound (LEFT, ci);
392         }
393
394       finished_line_spanner_ = 0;
395     }
396 }
397
398 void
399 Dynamic_engraver::acknowledge_accidental (Grob_info info)
400 {
401   if (line_spanner_)
402     Side_position_interface::add_support (line_spanner_, info.grob ());
403 }
404
405 void
406 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
407 {
408   if (line_spanner_)
409     Side_position_interface::add_support (line_spanner_, info.grob ());
410 }
411
412 void
413 Dynamic_engraver::acknowledge_slur (Grob_info info)
414 {
415   if (line_spanner_)
416     Side_position_interface::add_support (line_spanner_, info.grob ());
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 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
458 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
459 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
460 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
461
462 ADD_TRANSLATOR (Dynamic_engraver,
463                 /* doc */
464                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
465                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
466                 "which takes care of vertical positioning.  ",
467
468                 /* create */
469                 "DynamicLineSpanner "
470                 "DynamicText "
471                 "Hairpin "
472                 "TextSpanner ",
473
474                 /* read */ "",
475                 /* write */ "");