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