]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release commit
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.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 "interval.hh"
16 #include "note-column.hh"
17 #include "paper-column.hh"
18 #include "script-interface.hh"
19 #include "side-position-interface.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "warn.hh"
22 #include "self-alignment-interface.hh"
23 #include "pointer-group-interface.hh"
24
25 /*
26   TODO:
27
28   * direction of text-dynamic-event if not equal to direction of
29   line-spanner
30
31   - TODO: this engraver is too complicated. We should split it into
32   the handling of the basic grobs and the linespanner
33
34   - TODO: the line-spanner is not killed after the (de)crescs are
35   finished.
36 */
37
38 /**
39    print text & hairpin dynamics.
40 */
41 class Dynamic_engraver : public Engraver
42 {
43   Item *script_;
44   Spanner *line_spanner_;
45   Spanner *cresc_;
46
47   Spanner *finished_line_spanner_;
48   Spanner *finished_cresc_;
49
50   Music *script_ev_;
51   Music *current_cresc_ev_;
52
53   Drul_array<Music *> accepted_spanevents_drul_;
54
55   Link_array<Note_column> pending_columns_;
56   Link_array<Grob> pending_elements_;
57
58   void typeset_all ();
59
60   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
61
62 protected:
63   virtual void finalize ();
64   virtual void acknowledge_grob (Grob_info);
65   virtual bool try_music (Music *event);
66   PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
67   PRECOMPUTED_VIRTUAL void process_music ();
68 };
69
70 Dynamic_engraver::Dynamic_engraver ()
71 {
72   script_ = 0;
73   finished_cresc_ = 0;
74   line_spanner_ = 0;
75   finished_line_spanner_ = 0;
76   current_cresc_ev_ = 0;
77   cresc_ = 0;
78
79   script_ev_ = 0;
80   accepted_spanevents_drul_[START] = 0;
81   accepted_spanevents_drul_[STOP] = 0;
82 }
83
84 bool
85 Dynamic_engraver::try_music (Music *m)
86 {
87   if (m->is_mus_type ("absolute-dynamic-event"))
88     {
89       /*
90         TODO: probably broken.
91       */
92       script_ev_ = m;
93       return true;
94     }
95   else if (m->is_mus_type ("decrescendo-event")
96            || m->is_mus_type ("crescendo-event"))
97     {
98       Direction d = to_dir (m->get_property ("span-direction"));
99
100       accepted_spanevents_drul_[d] = m;
101       if (current_cresc_ev_ && d == START)
102         accepted_spanevents_drul_[STOP] = m;
103       return true;
104     }
105   return false;
106 }
107
108 void
109 Dynamic_engraver::process_music ()
110 {
111   if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
112     {
113       if (!line_spanner_)
114         {
115           Music *rq = accepted_spanevents_drul_[START];
116           line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
117
118           if (script_ev_)
119             rq = script_ev_;
120         }
121     }
122
123   /*
124     During a (de)crescendo, pending event will not be cleared,
125     and a line-spanner will always be created, as \< \! are already
126     two events.
127
128     Note: line-spanner must always have at least same duration
129     as (de)crecsendo, b.o. line-breaking.
130   */
131
132   /*
133     maybe we should leave dynamic texts to the text-engraver and
134     simply acknowledge them?
135   */
136   if (script_ev_)
137     {
138       script_ = make_item ("DynamicText", script_ev_->self_scm ());
139       script_->set_property ("text",
140                              script_ev_->get_property ("text"));
141
142       if (Direction d = to_dir (script_ev_->get_property ("direction")))
143         set_grob_direction (line_spanner_, d);
144
145       Axis_group_interface::add_element (line_spanner_, script_);
146     }
147
148   Music *stop_ev = accepted_spanevents_drul_ [STOP]
149     ? accepted_spanevents_drul_[STOP] : script_ev_;
150
151   if (accepted_spanevents_drul_[STOP] || script_ev_)
152     {
153       /*
154         finish side position alignment if the (de)cresc ends here, and
155         there are no new dynamics.
156       */
157
158       if (cresc_)
159         {
160           assert (!finished_cresc_ && cresc_);
161
162           if (script_)
163             {
164               cresc_->set_bound (RIGHT, script_);
165               add_bound_item (line_spanner_, script_);
166             }
167           
168           finished_cresc_ = cresc_;
169           cresc_ = 0;
170           current_cresc_ev_ = 0;
171         }
172       else if (accepted_spanevents_drul_[STOP])
173         {
174           accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
175           stop_ev = 0;
176         }
177     }
178
179   if (accepted_spanevents_drul_[START])
180     {
181       if (current_cresc_ev_)
182         {
183           String msg = _ ("already have a decrescendo");
184           if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
185             msg = _ ("already have a crescendo");
186
187           accepted_spanevents_drul_[START]->origin ()->warning (msg);
188           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
189         }
190       else
191         {
192           current_cresc_ev_ = accepted_spanevents_drul_[START];
193
194           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
195             set_grob_direction (line_spanner_, d);
196
197           /*
198             TODO: Use symbols.
199           */
200
201           String start_type
202             = ly_symbol2string (current_cresc_ev_->get_property ("name"));
203
204           /*
205             ugh. Use push/pop?
206           */
207           if (start_type == "DecrescendoEvent")
208             start_type = "decrescendo";
209           else if (start_type == "CrescendoEvent")
210             start_type = "crescendo";
211
212           SCM s = get_property ((start_type + "Spanner").to_str0 ());
213           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
214             {
215               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
216               if (finished_cresc_)
217                 {
218                   Pointer_group_interface::add_grob (finished_cresc_,
219                                                      ly_symbol2scm ("adjacent-hairpins"),
220                                                      cresc_);
221
222                   Pointer_group_interface::add_grob (cresc_,
223                                                      ly_symbol2scm ("adjacent-hairpins"),
224                                                      finished_cresc_);
225                 }
226               cresc_->set_property ("grow-direction",
227                                     scm_int2num ((start_type == "crescendo")
228                                                  ? BIGGER : SMALLER));
229             }
230
231           /*
232             This is a convenient (and legacy) interface to TextSpanners
233             for use in (de)crescendi.
234             Hmm.
235           */
236           else
237             {
238               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
239               cresc_->set_property ("style", s);
240               context ()->set_property ((start_type
241                                          + "Spanner").to_str0 (), SCM_EOL);
242               s = get_property ((start_type + "Text").to_str0 ());
243               /*
244                 FIXME: use get_markup () to check type.
245               */
246               if (scm_is_string (s) || scm_is_pair (s))
247                 {
248                   cresc_->set_property ("edge-text",
249                                         scm_cons (s, scm_makfrom0str ("")));
250                   context ()->set_property ((start_type + "Text").to_str0 (),
251                                             SCM_EOL);
252                 }
253             }
254
255           if (script_)
256             {
257               cresc_->set_bound (LEFT, script_);
258               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
259             }
260
261           Axis_group_interface::add_element (line_spanner_, cresc_);
262         }
263     }
264 }
265
266 void
267 Dynamic_engraver::stop_translation_timestep ()
268 {
269   if (!current_cresc_ev_ && line_spanner_)
270     {
271       assert (!finished_line_spanner_);
272       finished_line_spanner_ = line_spanner_;
273       line_spanner_ = 0;
274     }
275   
276   typeset_all ();
277
278   if (cresc_ && !cresc_->get_bound (LEFT))
279     {
280       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
281       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
282     }
283
284   script_ev_ = 0;
285   accepted_spanevents_drul_[START] = 0;
286   accepted_spanevents_drul_[STOP] = 0;
287 }
288
289 void
290 Dynamic_engraver::finalize ()
291 {
292   typeset_all ();
293
294   if (line_spanner_
295       && !line_spanner_->is_live ())
296     line_spanner_ = 0;
297   if (line_spanner_)
298     {
299       finished_line_spanner_ = line_spanner_;
300       typeset_all ();
301     }
302
303   if (cresc_
304       && !cresc_->is_live ())
305     cresc_ = 0;
306   if (cresc_)
307     {
308       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
309       cresc_->suicide ();
310       cresc_ = 0;
311     }
312 }
313
314 void
315 Dynamic_engraver::typeset_all ()
316 {
317   if (finished_cresc_)
318     {
319       if (!finished_cresc_->get_bound (RIGHT))
320         {
321           finished_cresc_->set_bound (RIGHT, script_
322                                       ? script_
323                                       : unsmob_grob (get_property ("currentMusicalColumn")));
324
325           if (finished_line_spanner_)
326             add_bound_item (finished_line_spanner_,
327                             finished_cresc_->get_bound (RIGHT));
328         }
329       finished_cresc_ = 0;
330     }
331
332   script_ = 0;
333   if (finished_line_spanner_)
334     {
335       /*
336         We used to have
337
338         extend-spanner-over-elements (finished_line_spanner_);
339
340         but this is rather kludgy, since finished_line_spanner_
341         typically has a staff-symbol field set , extending it over the
342         entire staff.
343
344       */
345
346       Grob *l = finished_line_spanner_->get_bound (LEFT);
347       Grob *r = finished_line_spanner_->get_bound (RIGHT);
348       if (!r && l)
349         finished_line_spanner_->set_bound (RIGHT, l);
350       else if (!l && r)
351         finished_line_spanner_->set_bound (LEFT, r);
352       else if (!r && !l)
353         {
354           /*
355             This is a isolated dynamic apparently, and does not even have
356             any interesting support item.
357           */
358           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
359           Item *ci = dynamic_cast<Item *> (cc);
360           finished_line_spanner_->set_bound (RIGHT, ci);
361           finished_line_spanner_->set_bound (LEFT, ci);
362         }
363
364       finished_line_spanner_ = 0;
365     }
366 }
367
368 void
369 Dynamic_engraver::acknowledge_grob (Grob_info info)
370 {
371   if (!line_spanner_)
372     return;
373
374   if (Note_column::has_interface (info.grob ()))
375     {
376       if (line_spanner_
377           /* Don't refill killed spanner */
378           && line_spanner_->is_live ())
379         {
380           Side_position_interface::add_support (line_spanner_, info.grob ());
381           add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
382         }
383
384       if (script_ && !script_->get_parent (X_AXIS))
385         {
386           extract_grob_set (info.grob (), "note-heads", heads);
387           if (heads.size())
388             {
389               Grob *head = heads[0];
390               script_->set_parent (head, X_AXIS);
391               script_->add_offset_callback (Self_alignment_interface::centered_on_parent_proc,
392                                             X_AXIS);
393
394             }
395         }
396
397       if (cresc_)
398         {
399           if (!cresc_->get_bound (LEFT))
400             {
401               cresc_->set_bound (LEFT, info.grob ());
402               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
403             }
404         }
405
406       if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
407         {
408           finished_cresc_->set_bound (RIGHT, info.grob ());
409         }
410     }
411   
412   else if (Script_interface::has_interface (info.grob ()) && script_)
413     {
414       SCM p = info.grob ()->get_property ("script-priority");
415
416       /*
417         UGH.
418
419         DynamicText doesn't really have a script-priority field.
420       */
421       if (scm_is_number (p)
422           && scm_to_int (p)
423           < scm_to_int (script_->get_property ("script-priority")))
424         Side_position_interface::add_support (line_spanner_, info.grob ());
425     }
426 }
427
428 #include "translator.icc"
429
430 ADD_TRANSLATOR (Dynamic_engraver,
431                 /* descr */
432                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
433                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
434                 "which takes care of vertical positioning.  ",
435
436                 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
437                 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
438                 /* acks  */ "note-column-interface script-interface",
439                 /* reads */ "",
440                 /* write */ "");