]> 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--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           cresc_->set_bound (RIGHT, script_
161                              ? script_
162                              : unsmob_grob (get_property ("currentMusicalColumn")));
163           add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
164
165           finished_cresc_ = cresc_;
166           cresc_ = 0;
167           current_cresc_ev_ = 0;
168         }
169       else if (accepted_spanreqs_drul_[STOP])
170         {
171           accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
172           stop_ev = 0;
173         }
174     }
175
176   if (accepted_spanreqs_drul_[START])
177     {
178       if (current_cresc_ev_)
179         {
180           String msg = _ ("already have a decrescendo");
181           if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
182             msg = _ ("already have a crescendo");
183
184           accepted_spanreqs_drul_[START]->origin ()->warning (msg);
185           current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
186         }
187       else
188         {
189           current_cresc_ev_ = accepted_spanreqs_drul_[START];
190
191           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
192             set_grob_direction (line_spanner_, d);
193
194           /*
195             TODO: Use symbols.
196           */
197
198           String start_type
199             = ly_symbol2string (current_cresc_ev_->get_property ("name"));
200
201           /*
202             ugh. Use push/pop?
203           */
204           if (start_type == "DecrescendoEvent")
205             start_type = "decrescendo";
206           else if (start_type == "CrescendoEvent")
207             start_type = "crescendo";
208
209           SCM s = get_property ((start_type + "Spanner").to_str0 ());
210           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
211             {
212               cresc_ = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
213               if (finished_cresc_)
214                 {
215                   Pointer_group_interface::add_grob (finished_cresc_,
216                                                      ly_symbol2scm ("adjacent-hairpins"),
217                                                      cresc_);
218
219                   Pointer_group_interface::add_grob (cresc_,
220                                                      ly_symbol2scm ("adjacent-hairpins"),
221                                                      finished_cresc_);
222                 }
223               cresc_->set_property ("grow-direction",
224                                     scm_int2num ((start_type == "crescendo")
225                                                  ? BIGGER : SMALLER));
226             }
227
228           /*
229             This is a convenient (and legacy) interface to TextSpanners
230             for use in (de)crescendi.
231             Hmm.
232           */
233           else
234             {
235               cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
236               cresc_->set_property ("style", s);
237               context ()->set_property ((start_type
238                                          + "Spanner").to_str0 (), SCM_EOL);
239               s = get_property ((start_type + "Text").to_str0 ());
240               /*
241                 FIXME: use get_markup () to check type.
242               */
243               if (scm_is_string (s) || scm_is_pair (s))
244                 {
245                   cresc_->set_property ("edge-text",
246                                         scm_cons (s, scm_makfrom0str ("")));
247                   context ()->set_property ((start_type + "Text").to_str0 (),
248                                             SCM_EOL);
249                 }
250             }
251
252           if (script_)
253             {
254               cresc_->set_bound (LEFT, script_);
255               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
256             }
257
258           Axis_group_interface::add_element (line_spanner_, cresc_);
259         }
260     }
261 }
262
263 void
264 Dynamic_engraver::stop_translation_timestep ()
265 {
266   typeset_all ();
267   if (!current_cresc_ev_)
268     {
269       finished_line_spanner_ = line_spanner_;
270       line_spanner_ = 0;
271       typeset_all ();
272     }
273
274   if (cresc_ && !cresc_->get_bound (LEFT))
275     {
276       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
277       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
278     }
279
280   script_ev_ = 0;
281   accepted_spanreqs_drul_[START] = 0;
282   accepted_spanreqs_drul_[STOP] = 0;
283 }
284
285 void
286 Dynamic_engraver::finalize ()
287 {
288   typeset_all ();
289
290   if (line_spanner_
291       && !line_spanner_->is_live ())
292     line_spanner_ = 0;
293   if (line_spanner_)
294     {
295       finished_line_spanner_ = line_spanner_;
296       typeset_all ();
297     }
298
299   if (cresc_
300       && !cresc_->is_live ())
301     cresc_ = 0;
302   if (cresc_)
303     {
304       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
305       cresc_->suicide ();
306       cresc_ = 0;
307     }
308 }
309
310 void
311 Dynamic_engraver::typeset_all ()
312 {
313   if (finished_cresc_)
314     {
315       if (!finished_cresc_->get_bound (RIGHT))
316         {
317           finished_cresc_->set_bound (RIGHT, script_
318                                       ? script_
319                                       : unsmob_grob (get_property ("currentMusicalColumn")));
320
321           if (finished_line_spanner_)
322             add_bound_item (finished_line_spanner_,
323                             finished_cresc_->get_bound (RIGHT));
324         }
325       finished_cresc_ = 0;
326     }
327
328   script_ = 0;
329   if (finished_line_spanner_)
330     {
331       /*
332         We used to have
333
334         extend-spanner-over-elements (finished_line_spanner_);
335
336         but this is rather kludgy, since finished_line_spanner_
337         typically has a staff-symbol field set , extending it over the
338         entire staff.
339
340       */
341
342       Grob *l = finished_line_spanner_->get_bound (LEFT);
343       Grob *r = finished_line_spanner_->get_bound (RIGHT);
344       if (!r && l)
345         finished_line_spanner_->set_bound (RIGHT, l);
346       else if (!l && r)
347         finished_line_spanner_->set_bound (LEFT, r);
348       else if (!r && !l)
349         {
350           /*
351             This is a isolated dynamic apparently, and does not even have
352             any interesting support item.
353           */
354           Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
355           Item *ci = dynamic_cast<Item *> (cc);
356           finished_line_spanner_->set_bound (RIGHT, ci);
357           finished_line_spanner_->set_bound (LEFT, ci);
358         }
359
360       finished_line_spanner_ = 0;
361     }
362 }
363
364 void
365 Dynamic_engraver::acknowledge_grob (Grob_info info)
366 {
367   if (!line_spanner_)
368     return;
369
370   if (Note_column::has_interface (info.grob_))
371     {
372       if (line_spanner_
373           /* Don't refill killed spanner */
374           && line_spanner_->is_live ())
375         {
376           Side_position_interface::add_support (line_spanner_, info.grob_);
377           add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob_));
378         }
379
380       if (script_ && !script_->get_parent (X_AXIS))
381         {
382           SCM head = scm_last_pair (info.grob_->get_property ("note-heads"));
383           if (scm_is_pair (head))
384             script_->set_parent (unsmob_grob (scm_car (head)), X_AXIS);
385         }
386
387       if (cresc_ && !cresc_->get_bound (LEFT))
388         {
389           cresc_->set_bound (LEFT, info.grob_);
390           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
391         }
392     }
393   else if (Script_interface::has_interface (info.grob_) && script_)
394     {
395       SCM p = info.grob_->get_property ("script-priority");
396
397       /*
398         UGH.
399
400         DynamicText doesn't really have a script-priority field.
401       */
402       if (scm_is_number (p)
403           && scm_to_int (p)
404           < scm_to_int (script_->get_property ("script-priority")))
405         Side_position_interface::add_support (line_spanner_, info.grob_);
406     }
407 }
408
409 ADD_TRANSLATOR (Dynamic_engraver,
410                 /* descr */
411                 "This engraver creates hairpins, dynamic texts, and their vertical\n"
412                 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
413                 "which takes care of vertical positioning.  ",
414
415                 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
416                 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
417                 /* acks  */ "note-column-interface script-interface",
418                 /* reads */ "",
419                 /* write */ "");