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