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