]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
use classnames for interface naming; remove inclusion of
[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 "item.hh"
27 #include "spanner.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 (script);
69   DECLARE_ACKNOWLEDGER (stem_tremolo);
70   DECLARE_ACKNOWLEDGER (note_column);
71   DECLARE_ACKNOWLEDGER (slur);
72   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
73   DECLARE_TRANSLATOR_LISTENER (span_dynamic);
74
75 protected:
76   virtual void finalize ();
77   void stop_translation_timestep ();
78   void process_music ();
79 };
80
81 Dynamic_engraver::Dynamic_engraver ()
82 {
83   script_ = 0;
84   finished_cresc_ = 0;
85   line_spanner_ = 0;
86   finished_line_spanner_ = 0;
87   current_cresc_ev_ = 0;
88   cresc_ = 0;
89
90   script_ev_ = 0;
91   accepted_spanevents_drul_[START] = 0;
92   accepted_spanevents_drul_[STOP] = 0;
93 }
94
95 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
96 void
97 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
98 {
99   /*
100     TODO: probably broken.
101   */
102   ASSIGN_EVENT_ONCE (script_ev_, ev);
103 }
104
105 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
106 void
107 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
108 {
109   Direction d = to_dir (ev->get_property ("span-direction"));
110
111   if (d == START)
112     ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
113   
114   /* Cancel any ongoing crescendo, either explicitly by \! or
115      implicitly by a new crescendo. Also avoid warning if cresc is
116      cancelled both implicitly and explicitly. */
117   if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
118     ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
119 }
120
121 void
122 Dynamic_engraver::process_music ()
123 {
124   if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
125     {
126       if (!line_spanner_)
127         {
128           Stream_event *rq = accepted_spanevents_drul_[START];
129           line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
130           if (script_ev_)
131             rq = script_ev_;
132         }
133     }
134
135   /*
136     During a (de)crescendo, pending event will not be cleared,
137     and a line-spanner will always be created, as \< \! are already
138     two events.
139
140     Note: line-spanner must always have at least same duration
141     as (de)crecsendo, b.o. line-breaking.
142   */
143
144   /*
145     maybe we should leave dynamic texts to the text-engraver and
146     simply acknowledge them?
147   */
148   if (script_ev_)
149     {
150       script_ = make_item ("DynamicText", script_ev_->self_scm ());
151       script_->set_property ("text",
152                              script_ev_->get_property ("text"));
153
154       if (Direction d = to_dir (script_ev_->get_property ("direction")))
155         set_grob_direction (line_spanner_, d);
156       else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
157         set_grob_direction (script_, d);
158
159       Axis_group_interface::add_element (line_spanner_, script_);
160     }
161
162   Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
163     ? accepted_spanevents_drul_[STOP] : script_ev_;
164
165   if (accepted_spanevents_drul_[STOP] || script_ev_)
166     {
167       /*
168         finish side position alignment if the (de)cresc ends here, and
169         there are no new dynamics.
170       */
171
172       if (cresc_)
173         {
174           assert (!finished_cresc_ && cresc_);
175
176           if (script_)
177             {
178               cresc_->set_bound (RIGHT, script_);
179               add_bound_item (line_spanner_, script_);
180             }
181
182           finished_cresc_ = cresc_;
183           cresc_ = 0;
184           current_cresc_ev_ = 0;
185         }
186       else if (accepted_spanevents_drul_[STOP])
187         {
188           accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
189           stop_ev = 0;
190         }
191     }
192
193   if (accepted_spanevents_drul_[START])
194     {
195       if (current_cresc_ev_)
196         {
197           string msg = _ ("already have a decrescendo");
198           if (current_cresc_ev_->in_event_class ("crescendo-event"))
199             msg = _ ("already have a crescendo");
200
201           accepted_spanevents_drul_[START]->origin ()->warning (msg);
202           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
203         }
204       else
205         {
206           current_cresc_ev_ = accepted_spanevents_drul_[START];
207
208           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
209             set_grob_direction (line_spanner_, d);
210
211           /*
212             TODO: Use symbols.
213           */
214           
215           SCM start_sym = current_cresc_ev_->get_property ("class");
216           string start_type;
217           
218           if (start_sym == ly_symbol2scm ("decrescendo-event"))
219             start_type = "decrescendo";
220           else if (start_sym == ly_symbol2scm ("crescendo-event"))
221             start_type = "crescendo";
222           else
223             {
224               programming_error ("unknown dynamic spanner type");
225               return;
226             }
227
228           /*
229             UGH. TODO: should read from original event, so appearance
230             may be altered with \tweak.
231            */
232           SCM s = get_property ((start_type + "Spanner").c_str ());
233           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
234             {
235               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
236               if (finished_cresc_)
237                 {
238                   Pointer_group_interface::add_grob (finished_cresc_,
239                                                      ly_symbol2scm ("adjacent-hairpins"),
240                                                      cresc_);
241
242                   Pointer_group_interface::add_grob (cresc_,
243                                                      ly_symbol2scm ("adjacent-hairpins"),
244                                                      finished_cresc_);
245                 }
246             }
247
248           /*
249             This is a convenient (and legacy) interface to TextSpanners
250             for use in (de)crescendi.
251             Hmm.
252           */
253           else
254             {
255               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
256               cresc_->set_property ("style", s);
257               context ()->set_property ((start_type
258                                          + "Spanner").c_str (), SCM_EOL);
259               s = get_property ((start_type + "Text").c_str ());
260               /*
261                 FIXME: use get_markup () to check type.
262               */
263               if (scm_is_string (s) || scm_is_pair (s))
264                 {
265                   cresc_->set_property ("edge-text",
266                                         scm_cons (s, scm_makfrom0str ("")));
267                   context ()->set_property ((start_type + "Text").c_str (),
268                                             SCM_EOL);
269                 }
270             }
271
272           if (script_)
273             {
274               cresc_->set_bound (LEFT, script_);
275               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
276             }
277
278           Axis_group_interface::add_element (line_spanner_, cresc_);
279         }
280     }
281 }
282
283 void
284 Dynamic_engraver::stop_translation_timestep ()
285 {
286   if (!current_cresc_ev_ && line_spanner_)
287     {
288       assert (!finished_line_spanner_);
289       finished_line_spanner_ = line_spanner_;
290       line_spanner_ = 0;
291     }
292
293   typeset_all ();
294
295   if (cresc_ && !cresc_->get_bound (LEFT))
296     {
297       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
298       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
299     }
300
301   script_ev_ = 0;
302   accepted_spanevents_drul_[START] = 0;
303   accepted_spanevents_drul_[STOP] = 0;
304 }
305
306 void
307 Dynamic_engraver::finalize ()
308 {
309   typeset_all ();
310
311   if (line_spanner_
312       && !line_spanner_->is_live ())
313     line_spanner_ = 0;
314   if (line_spanner_)
315     {
316       finished_line_spanner_ = line_spanner_;
317       typeset_all ();
318     }
319
320   if (cresc_
321       && !cresc_->is_live ())
322     cresc_ = 0;
323   if (cresc_)
324     {
325       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
326       cresc_->suicide ();
327       cresc_ = 0;
328     }
329 }
330
331 void
332 Dynamic_engraver::typeset_all ()
333 {
334   if (finished_cresc_)
335     {
336       bool use_bar = to_boolean (get_property ("hairpinToBarline"))
337         && scm_is_string (get_property ("whichBar"))
338         && !script_ev_;
339                           
340       
341       if (!finished_cresc_->get_bound (RIGHT)
342           || use_bar)
343         {
344           Grob *column_bound = unsmob_grob (use_bar
345                                             ? get_property ("currentCommandColumn")
346                                             : get_property ("currentMusicalColumn"));
347             
348           finished_cresc_->set_bound (RIGHT, script_
349                                       ? script_
350                                       : column_bound);
351
352           if (finished_line_spanner_)
353             add_bound_item (finished_line_spanner_,
354                             finished_cresc_->get_bound (RIGHT));
355         }
356       finished_cresc_ = 0;
357     }
358
359   script_ = 0;
360   if (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       finished_line_spanner_ = 0;
392     }
393 }
394
395
396 void
397 Dynamic_engraver::acknowledge_accidental (Grob_info info)
398 {
399   if (line_spanner_)
400     Side_position_interface::add_support (line_spanner_, info.grob ());
401 }
402
403
404 void
405 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
406 {
407   if (line_spanner_)
408     Side_position_interface::add_support (line_spanner_, info.grob ());
409 }
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
420 void
421 Dynamic_engraver::acknowledge_note_column (Grob_info info)
422 {
423   if (!line_spanner_)
424     return;
425
426   if (line_spanner_
427       /* Don't refill killed spanner */
428       && line_spanner_->is_live ())
429     {
430       Side_position_interface::add_support (line_spanner_, info.grob ());
431       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
432     }
433
434   if (script_ && !script_->get_parent (X_AXIS))
435     {
436       extract_grob_set (info.grob (), "note-heads", heads);
437       if (heads.size ())
438         {
439           Grob *head = heads[0];
440           script_->set_parent (head, X_AXIS);
441           Self_alignment_interface::set_center_parent (script_, X_AXIS);
442         }
443     }
444
445   if (cresc_)
446     {
447       if (!cresc_->get_bound (LEFT))
448         {
449           cresc_->set_bound (LEFT, info.grob ());
450           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
451         }
452     }
453
454   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
455     finished_cresc_->set_bound (RIGHT, info.grob ());
456 }
457
458 void
459 Dynamic_engraver::acknowledge_script (Grob_info info)
460 {
461   if (!line_spanner_ || !script_)
462     return;
463
464   SCM p = info.grob ()->get_property ("script-priority");
465
466   /*
467     UGH.
468
469     DynamicText doesn't really have a script-priority field.
470   */
471   if (scm_is_number (p)
472       && scm_to_int (p)
473       < scm_to_int (script_->get_property ("script-priority")))
474     Side_position_interface::add_support (line_spanner_, info.grob ());
475 }
476
477 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
478 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
479 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
480 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
481 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
482
483 ADD_TRANSLATOR (Dynamic_engraver,
484                 /* doc */
485                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
486                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
487                 "which takes care of vertical positioning.  ",
488
489                 /* create */
490                 "DynamicLineSpanner "
491                 "DynamicText "
492                 "Hairpin "
493                 "TextSpanner ",
494
495                 /* read */ "",
496                 /* write */ "");