]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
Merge branch 'master' of ssh+git://gpercival@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
180           /* backwards compatibility with hairpinToBarline */
181           bool use_bar = to_boolean (get_property ("hairpinToBarline"))
182             && scm_is_string (get_property ("whichBar"))
183             && !script_ev_;
184
185           finished_cresc_->set_property ("to-barline", scm_from_bool (use_bar));
186
187           announce_end_grob (finished_cresc_, SCM_EOL);
188           cresc_ = 0;
189           current_cresc_ev_ = 0;
190         }
191       else if (accepted_spanevents_drul_[STOP])
192         {
193           accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
194           stop_ev = 0;
195         }
196     }
197
198   if (accepted_spanevents_drul_[START])
199     {
200       if (current_cresc_ev_)
201         {
202           string msg = _ ("already have a decrescendo");
203           if (current_cresc_ev_->in_event_class ("crescendo-event"))
204             msg = _ ("already have a crescendo");
205
206           accepted_spanevents_drul_[START]->origin ()->warning (msg);
207           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
208         }
209       else
210         {
211           current_cresc_ev_ = accepted_spanevents_drul_[START];
212
213           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
214             set_grob_direction (line_spanner_, d);
215
216           /*
217             TODO: Use symbols.
218           */
219           
220           SCM start_sym = current_cresc_ev_->get_property ("class");
221           string start_type;
222           
223           if (start_sym == ly_symbol2scm ("decrescendo-event"))
224             start_type = "decrescendo";
225           else if (start_sym == ly_symbol2scm ("crescendo-event"))
226             start_type = "crescendo";
227           else
228             {
229               programming_error ("unknown dynamic spanner type");
230               return;
231             }
232
233           /*
234             UGH. TODO: should read from original event, so appearance
235             may be altered with \tweak.
236            */
237           SCM s = get_property ((start_type + "Spanner").c_str ());
238           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
239             {
240               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
241               if (finished_cresc_)
242                 {
243                   Pointer_group_interface::add_grob (finished_cresc_,
244                                                      ly_symbol2scm ("adjacent-hairpins"),
245                                                      cresc_);
246
247                   Pointer_group_interface::add_grob (cresc_,
248                                                      ly_symbol2scm ("adjacent-hairpins"),
249                                                      finished_cresc_);
250                 }
251             }
252
253           /*
254             This is a convenient (and legacy) interface to TextSpanners
255             for use in (de)crescendi.
256             Hmm.
257           */
258           else
259             {
260               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
261               cresc_->set_property ("style", s);
262               context ()->set_property ((start_type
263                                          + "Spanner").c_str (), SCM_EOL);
264               s = get_property ((start_type + "Text").c_str ());
265               if (Text_interface::is_markup (s))
266                 {
267                   cresc_->set_property ("text", s);
268                   context ()->set_property ((start_type + "Text").c_str (),
269                                             SCM_EOL);
270                 }
271
272               if (script_)
273                 {
274                   set_nested_property (cresc_,
275                                        scm_list_3 (ly_symbol2scm ("bound-details"),
276                                                    ly_symbol2scm ("left"),
277                                                    ly_symbol2scm ("attach-dir")
278                                                    ),
279                                        scm_from_int (RIGHT));
280                 }
281             }
282
283           if (script_)
284             {
285               cresc_->set_bound (LEFT, script_);
286               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
287             }
288
289           Axis_group_interface::add_element (line_spanner_, cresc_);
290         }
291     }
292 }
293
294 void
295 Dynamic_engraver::stop_translation_timestep ()
296 {
297   if (!current_cresc_ev_ && line_spanner_)
298     {
299       assert (!finished_line_spanner_);
300       finished_line_spanner_ = line_spanner_;
301       line_spanner_ = 0;
302     }
303
304   typeset_all ();
305
306   if (cresc_ && !cresc_->get_bound (LEFT))
307     {
308       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
309       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
310     }
311
312   script_ev_ = 0;
313   accepted_spanevents_drul_[START] = 0;
314   accepted_spanevents_drul_[STOP] = 0;
315 }
316
317 void
318 Dynamic_engraver::finalize ()
319 {
320   typeset_all ();
321
322   if (line_spanner_
323       && !line_spanner_->is_live ())
324     line_spanner_ = 0;
325   if (line_spanner_)
326     {
327       finished_line_spanner_ = line_spanner_;
328       typeset_all ();
329     }
330
331   if (cresc_
332       && !cresc_->is_live ())
333     cresc_ = 0;
334   if (cresc_)
335     {
336       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
337       cresc_->suicide ();
338       cresc_ = 0;
339     }
340 }
341
342 void
343 Dynamic_engraver::typeset_all ()
344 {
345   if (finished_cresc_)
346     {
347       if (!finished_cresc_->get_bound (RIGHT))
348         {
349                   
350           Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
351           
352           finished_cresc_->set_bound (RIGHT, script_
353                                       ? script_
354                                       : column_bound);
355
356           if (finished_line_spanner_)
357             add_bound_item (finished_line_spanner_,
358                             finished_cresc_->get_bound (RIGHT));
359         }
360       finished_cresc_ = 0;
361     }
362
363   script_ = 0;
364   if (finished_line_spanner_)
365     {
366       /*
367         We used to have
368
369         extend-spanner-over-elements (finished_line_spanner_);
370
371         but this is rather kludgy, since finished_line_spanner_
372         typically has a staff-symbol field set , extending it over the
373         entire staff.
374
375       */
376
377       Grob *l = finished_line_spanner_->get_bound (LEFT);
378       Grob *r = finished_line_spanner_->get_bound (RIGHT);
379       if (!r && l)
380         finished_line_spanner_->set_bound (RIGHT, l);
381       else if (!l && r)
382         finished_line_spanner_->set_bound (LEFT, r);
383       else if (!r && !l)
384         {
385           /*
386             This is a isolated dynamic apparently, and does not even have
387             any interesting support item.
388           */
389           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
390           Item *ci = dynamic_cast<Item *> (cc);
391           finished_line_spanner_->set_bound (RIGHT, ci);
392           finished_line_spanner_->set_bound (LEFT, ci);
393         }
394       finished_line_spanner_ = 0;
395     }
396 }
397
398 void
399 Dynamic_engraver::acknowledge_note_column (Grob_info info)
400 {
401   if (!line_spanner_)
402     return;
403
404   if (line_spanner_
405       /* Don't refill killed spanner */
406       && line_spanner_->is_live ())
407     {
408       Side_position_interface::add_support (line_spanner_, info.grob ());
409       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
410     }
411
412   if (script_ && !script_->get_parent (X_AXIS))
413     {
414       extract_grob_set (info.grob (), "note-heads", heads);
415       if (heads.size ())
416         {
417           Grob *head = heads[0];
418           script_->set_parent (head, X_AXIS);
419           Self_alignment_interface::set_center_parent (script_, X_AXIS);
420         }
421     }
422
423   if (cresc_)
424     {
425       if (!cresc_->get_bound (LEFT))
426         {
427           cresc_->set_bound (LEFT, info.grob ());
428           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
429         }
430     }
431
432   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
433     finished_cresc_->set_bound (RIGHT, info.grob ());
434 }
435
436 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
437
438 ADD_TRANSLATOR (Dynamic_engraver,
439                 /* doc */
440                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
441                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
442                 "which takes care of vertical positioning.  ",
443
444                 /* create */
445                 "DynamicLineSpanner "
446                 "DynamicTextSpanner "
447                 "DynamicText "
448                 "Hairpin "
449                 "TextSpanner ",
450
451                 /* read */ "",
452                 /* write */ "");