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