]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
daba7713865d3c82d43b6a8f8603992fb23a852d
[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             }
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_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
254               cresc_->set_property ("style", s);
255               context ()->set_property ((start_type
256                                          + "Spanner").c_str (), SCM_EOL);
257               s = get_property ((start_type + "Text").c_str ());
258               /*
259                 FIXME: use get_markup () to check type.
260               */
261               if (scm_is_string (s) || scm_is_pair (s))
262                 {
263                   cresc_->set_property ("edge-text",
264                                         scm_cons (s, scm_makfrom0str ("")));
265                   context ()->set_property ((start_type + "Text").c_str (),
266                                             SCM_EOL);
267                 }
268             }
269
270           if (script_)
271             {
272               cresc_->set_bound (LEFT, script_);
273               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
274             }
275
276           Axis_group_interface::add_element (line_spanner_, cresc_);
277         }
278     }
279 }
280
281 void
282 Dynamic_engraver::stop_translation_timestep ()
283 {
284   if (!current_cresc_ev_ && line_spanner_)
285     {
286       assert (!finished_line_spanner_);
287       finished_line_spanner_ = line_spanner_;
288       line_spanner_ = 0;
289     }
290
291   typeset_all ();
292
293   if (cresc_ && !cresc_->get_bound (LEFT))
294     {
295       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
296       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
297     }
298
299   script_ev_ = 0;
300   accepted_spanevents_drul_[START] = 0;
301   accepted_spanevents_drul_[STOP] = 0;
302 }
303
304 void
305 Dynamic_engraver::finalize ()
306 {
307   typeset_all ();
308
309   if (line_spanner_
310       && !line_spanner_->is_live ())
311     line_spanner_ = 0;
312   if (line_spanner_)
313     {
314       finished_line_spanner_ = line_spanner_;
315       typeset_all ();
316     }
317
318   if (cresc_
319       && !cresc_->is_live ())
320     cresc_ = 0;
321   if (cresc_)
322     {
323       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
324       cresc_->suicide ();
325       cresc_ = 0;
326     }
327 }
328
329 void
330 Dynamic_engraver::typeset_all ()
331 {
332   if (finished_cresc_)
333     {
334       bool use_bar = to_boolean (get_property ("hairpinToBarline"))
335         && scm_is_string (get_property ("whichBar"))
336         && !script_ev_;
337                           
338       
339       if (!finished_cresc_->get_bound (RIGHT)
340           || use_bar)
341         {
342           Grob *column_bound = unsmob_grob (use_bar
343                                             ? get_property ("currentCommandColumn")
344                                             : get_property ("currentMusicalColumn"));
345             
346           finished_cresc_->set_bound (RIGHT, script_
347                                       ? script_
348                                       : column_bound);
349
350           if (finished_line_spanner_)
351             add_bound_item (finished_line_spanner_,
352                             finished_cresc_->get_bound (RIGHT));
353         }
354       finished_cresc_ = 0;
355     }
356
357   script_ = 0;
358   if (finished_line_spanner_)
359     {
360       /*
361         We used to have
362
363         extend-spanner-over-elements (finished_line_spanner_);
364
365         but this is rather kludgy, since finished_line_spanner_
366         typically has a staff-symbol field set , extending it over the
367         entire staff.
368
369       */
370
371       Grob *l = finished_line_spanner_->get_bound (LEFT);
372       Grob *r = finished_line_spanner_->get_bound (RIGHT);
373       if (!r && l)
374         finished_line_spanner_->set_bound (RIGHT, l);
375       else if (!l && r)
376         finished_line_spanner_->set_bound (LEFT, r);
377       else if (!r && !l)
378         {
379           /*
380             This is a isolated dynamic apparently, and does not even have
381             any interesting support item.
382           */
383           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
384           Item *ci = dynamic_cast<Item *> (cc);
385           finished_line_spanner_->set_bound (RIGHT, ci);
386           finished_line_spanner_->set_bound (LEFT, ci);
387         }
388
389       finished_line_spanner_ = 0;
390     }
391 }
392
393
394 void
395 Dynamic_engraver::acknowledge_accidental (Grob_info info)
396 {
397   if (line_spanner_)
398     Side_position_interface::add_support (line_spanner_, info.grob ());
399 }
400
401
402 void
403 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
404 {
405   if (line_spanner_)
406     Side_position_interface::add_support (line_spanner_, info.grob ());
407 }
408
409
410 void
411 Dynamic_engraver::acknowledge_slur (Grob_info info)
412 {
413   if (line_spanner_)
414     Side_position_interface::add_support (line_spanner_, info.grob ());
415 }
416
417
418 void
419 Dynamic_engraver::acknowledge_note_column (Grob_info info)
420 {
421   if (!line_spanner_)
422     return;
423
424   if (line_spanner_
425       /* Don't refill killed spanner */
426       && line_spanner_->is_live ())
427     {
428       Side_position_interface::add_support (line_spanner_, info.grob ());
429       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
430     }
431
432   if (script_ && !script_->get_parent (X_AXIS))
433     {
434       extract_grob_set (info.grob (), "note-heads", heads);
435       if (heads.size ())
436         {
437           Grob *head = heads[0];
438           script_->set_parent (head, X_AXIS);
439           Self_alignment_interface::set_center_parent (script_, X_AXIS);
440         }
441     }
442
443   if (cresc_)
444     {
445       if (!cresc_->get_bound (LEFT))
446         {
447           cresc_->set_bound (LEFT, info.grob ());
448           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
449         }
450     }
451
452   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
453     finished_cresc_->set_bound (RIGHT, info.grob ());
454 }
455
456 void
457 Dynamic_engraver::acknowledge_script (Grob_info info)
458 {
459   if (!line_spanner_ || !script_)
460     return;
461
462   SCM p = info.grob ()->get_property ("script-priority");
463
464   /*
465     UGH.
466
467     DynamicText doesn't really have a script-priority field.
468   */
469   if (scm_is_number (p)
470       && scm_to_int (p)
471       < scm_to_int (script_->get_property ("script-priority")))
472     Side_position_interface::add_support (line_spanner_, info.grob ());
473 }
474
475 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
476 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
477 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
478 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
479 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
480
481 ADD_TRANSLATOR (Dynamic_engraver,
482                 /* doc */
483                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
484                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
485                 "which takes care of vertical positioning.  ",
486
487                 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
488                 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",
489                 /* read */ "",
490                 /* write */ "");