]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
(acknowledge_grob): only center on
[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
24 /*
25   TODO:
26
27   * direction of text-dynamic-event if not equal to direction of
28   line-spanner
29
30   - TODO: this engraver is too complicated. We should split it into
31   the handling of the basic grobs and the linespanner
32
33   - TODO: the line-spanner is not killed after the (de)crescs are
34   finished.
35 */
36
37 /**
38    print text & hairpin dynamics.
39 */
40 class Dynamic_engraver : public Engraver
41 {
42   Item *script_;
43   Spanner *line_spanner_;
44   Spanner *cresc_;
45
46   Spanner *finished_line_spanner_;
47   Spanner *finished_cresc_;
48
49   Music *script_ev_;
50   Music *current_cresc_ev_;
51
52   Drul_array<Music *> accepted_spanreqs_drul_;
53
54   Link_array<Note_column> pending_columns_;
55   Link_array<Grob> pending_elements_;
56
57   void typeset_all ();
58
59   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
60
61 protected:
62   virtual void finalize ();
63   virtual void acknowledge_grob (Grob_info);
64   virtual bool try_music (Music *req);
65   virtual void stop_translation_timestep ();
66   virtual void process_music ();
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           if (script_)
162             {
163               cresc_->set_bound (RIGHT, script_);
164               add_bound_item (line_spanner_, script_);
165             }
166           
167           finished_cresc_ = cresc_;
168           cresc_ = 0;
169           current_cresc_ev_ = 0;
170         }
171       else if (accepted_spanreqs_drul_[STOP])
172         {
173           accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
174           stop_ev = 0;
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 starts 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             This is a convenient (and legacy) interface to TextSpanners
232             for use in (de)crescendi.
233             Hmm.
234           */
235           else
236             {
237               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
238               cresc_->set_property ("style", s);
239               context ()->set_property ((start_type
240                                          + "Spanner").to_str0 (), SCM_EOL);
241               s = get_property ((start_type + "Text").to_str0 ());
242               /*
243                 FIXME: use get_markup () to check type.
244               */
245               if (scm_is_string (s) || scm_is_pair (s))
246                 {
247                   cresc_->set_property ("edge-text",
248                                         scm_cons (s, scm_makfrom0str ("")));
249                   context ()->set_property ((start_type + "Text").to_str0 (),
250                                             SCM_EOL);
251                 }
252             }
253
254           if (script_)
255             {
256               cresc_->set_bound (LEFT, script_);
257               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
258             }
259
260           Axis_group_interface::add_element (line_spanner_, cresc_);
261         }
262     }
263 }
264
265 void
266 Dynamic_engraver::stop_translation_timestep ()
267 {
268   if (!current_cresc_ev_ && line_spanner_)
269     {
270       assert (!finished_line_spanner_);
271       finished_line_spanner_ = line_spanner_;
272       line_spanner_ = 0;
273     }
274   
275   typeset_all ();
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             {
388               script_->set_parent (unsmob_grob (scm_car (head)), X_AXIS);
389               script_->add_offset_callback (Self_alignment_interface::centered_on_parent_proc,
390                                             X_AXIS);
391
392             }
393         }
394
395       if (cresc_)
396         {
397           if (!cresc_->get_bound (LEFT))
398             {
399               cresc_->set_bound (LEFT, info.grob ());
400               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
401             }
402         }
403
404       if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
405         {
406           finished_cresc_->set_bound (RIGHT, info.grob ());
407         }
408     }
409   
410   else if (Script_interface::has_interface (info.grob ()) && script_)
411     {
412       SCM p = info.grob ()->get_property ("script-priority");
413
414       /*
415         UGH.
416
417         DynamicText doesn't really have a script-priority field.
418       */
419       if (scm_is_number (p)
420           && scm_to_int (p)
421           < scm_to_int (script_->get_property ("script-priority")))
422         Side_position_interface::add_support (line_spanner_, info.grob ());
423     }
424 }
425
426 ADD_TRANSLATOR (Dynamic_engraver,
427                 /* descr */
428                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
429                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
430                 "which takes care of vertical positioning.  ",
431
432                 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
433                 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
434                 /* acks  */ "note-column-interface script-interface",
435                 /* reads */ "",
436                 /* write */ "");