]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
*** empty log message ***
[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           if (start_type == "DecrescendoEvent")
212             start_type = "decrescendo";
213           else if (start_type == "CrescendoEvent")
214             start_type = "crescendo";
215           
216                                        
217
218           /*
219             UGH. TODO: should read from original event, so appearance
220             may be altered with \tweak.
221            */
222           SCM s = get_property ((start_type + "Spanner").c_str ());
223           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
224             {
225               cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
226               if (finished_cresc_)
227                 {
228                   Pointer_group_interface::add_grob (finished_cresc_,
229                                                      ly_symbol2scm ("adjacent-hairpins"),
230                                                      cresc_);
231
232                   Pointer_group_interface::add_grob (cresc_,
233                                                      ly_symbol2scm ("adjacent-hairpins"),
234                                                      finished_cresc_);
235                 }
236               cresc_->set_property ("grow-direction",
237                                     scm_from_int ((start_type == "crescendo")
238                                                   ? BIGGER : SMALLER));
239             }
240
241           /*
242             This is a convenient (and legacy) interface to TextSpanners
243             for use in (de)crescendi.
244             Hmm.
245           */
246           else
247             {
248               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
249               cresc_->set_property ("style", s);
250               context ()->set_property ((start_type
251                                          + "Spanner").c_str (), SCM_EOL);
252               s = get_property ((start_type + "Text").c_str ());
253               /*
254                 FIXME: use get_markup () to check type.
255               */
256               if (scm_is_string (s) || scm_is_pair (s))
257                 {
258                   cresc_->set_property ("edge-text",
259                                         scm_cons (s, scm_makfrom0str ("")));
260                   context ()->set_property ((start_type + "Text").c_str (),
261                                             SCM_EOL);
262                 }
263             }
264
265           if (script_)
266             {
267               cresc_->set_bound (LEFT, script_);
268               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
269             }
270
271           Axis_group_interface::add_element (line_spanner_, cresc_);
272         }
273     }
274 }
275
276 void
277 Dynamic_engraver::stop_translation_timestep ()
278 {
279   if (!current_cresc_ev_ && line_spanner_)
280     {
281       assert (!finished_line_spanner_);
282       finished_line_spanner_ = line_spanner_;
283       line_spanner_ = 0;
284     }
285
286   typeset_all ();
287
288   if (cresc_ && !cresc_->get_bound (LEFT))
289     {
290       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
291       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
292     }
293
294   script_ev_ = 0;
295   accepted_spanevents_drul_[START] = 0;
296   accepted_spanevents_drul_[STOP] = 0;
297 }
298
299 void
300 Dynamic_engraver::finalize ()
301 {
302   typeset_all ();
303
304   if (line_spanner_
305       && !line_spanner_->is_live ())
306     line_spanner_ = 0;
307   if (line_spanner_)
308     {
309       finished_line_spanner_ = line_spanner_;
310       typeset_all ();
311     }
312
313   if (cresc_
314       && !cresc_->is_live ())
315     cresc_ = 0;
316   if (cresc_)
317     {
318       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
319       cresc_->suicide ();
320       cresc_ = 0;
321     }
322 }
323
324 void
325 Dynamic_engraver::typeset_all ()
326 {
327   if (finished_cresc_)
328     {
329       bool use_bar = to_boolean (get_property ("hairpinToBarline"))
330         && scm_is_string (get_property ("whichBar"))
331         && !script_ev_;
332                           
333       
334       if (!finished_cresc_->get_bound (RIGHT)
335           || use_bar)
336         {
337           Grob *column_bound = unsmob_grob (use_bar
338                                             ? get_property ("currentCommandColumn")
339                                             : get_property ("currentMusicalColumn"));
340             
341           finished_cresc_->set_bound (RIGHT, script_
342                                       ? script_
343                                       : column_bound);
344
345           if (finished_line_spanner_)
346             add_bound_item (finished_line_spanner_,
347                             finished_cresc_->get_bound (RIGHT));
348         }
349       finished_cresc_ = 0;
350     }
351
352   script_ = 0;
353   if (finished_line_spanner_)
354     {
355       /*
356         We used to have
357
358         extend-spanner-over-elements (finished_line_spanner_);
359
360         but this is rather kludgy, since finished_line_spanner_
361         typically has a staff-symbol field set , extending it over the
362         entire staff.
363
364       */
365
366       Grob *l = finished_line_spanner_->get_bound (LEFT);
367       Grob *r = finished_line_spanner_->get_bound (RIGHT);
368       if (!r && l)
369         finished_line_spanner_->set_bound (RIGHT, l);
370       else if (!l && r)
371         finished_line_spanner_->set_bound (LEFT, r);
372       else if (!r && !l)
373         {
374           /*
375             This is a isolated dynamic apparently, and does not even have
376             any interesting support item.
377           */
378           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
379           Item *ci = dynamic_cast<Item *> (cc);
380           finished_line_spanner_->set_bound (RIGHT, ci);
381           finished_line_spanner_->set_bound (LEFT, ci);
382         }
383
384       finished_line_spanner_ = 0;
385     }
386 }
387
388
389 void
390 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
391 {
392   if (line_spanner_)
393     Side_position_interface::add_support (line_spanner_, info.grob ());
394 }
395
396
397 void
398 Dynamic_engraver::acknowledge_slur (Grob_info info)
399 {
400   if (line_spanner_)
401     Side_position_interface::add_support (line_spanner_, info.grob ());
402 }
403
404
405 void
406 Dynamic_engraver::acknowledge_note_column (Grob_info info)
407 {
408   if (!line_spanner_)
409     return;
410
411   if (line_spanner_
412       /* Don't refill killed spanner */
413       && line_spanner_->is_live ())
414     {
415       Side_position_interface::add_support (line_spanner_, info.grob ());
416       add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
417     }
418
419   if (script_ && !script_->get_parent (X_AXIS))
420     {
421       extract_grob_set (info.grob (), "note-heads", heads);
422       if (heads.size ())
423         {
424           Grob *head = heads[0];
425           script_->set_parent (head, X_AXIS);
426           Self_alignment_interface::set_center_parent (script_, X_AXIS);
427         }
428     }
429
430   if (cresc_)
431     {
432       if (!cresc_->get_bound (LEFT))
433         {
434           cresc_->set_bound (LEFT, info.grob ());
435           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
436         }
437     }
438
439   if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
440     finished_cresc_->set_bound (RIGHT, info.grob ());
441 }
442
443 void
444 Dynamic_engraver::acknowledge_script (Grob_info info)
445 {
446   if (!line_spanner_ || !script_)
447     return;
448
449   SCM p = info.grob ()->get_property ("script-priority");
450
451   /*
452     UGH.
453
454     DynamicText doesn't really have a script-priority field.
455   */
456   if (scm_is_number (p)
457       && scm_to_int (p)
458       < scm_to_int (script_->get_property ("script-priority")))
459     Side_position_interface::add_support (line_spanner_, info.grob ());
460 }
461
462 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
463 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
464 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
465 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
466
467 ADD_TRANSLATOR (Dynamic_engraver,
468                 /* doc */
469                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
470                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
471                 "which takes care of vertical positioning.  ",
472
473                 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
474                 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",
475                 /* read */ "",
476                 /* write */ "");