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