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