2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "axis-group-interface.hh"
11 #include "dimensions.hh"
12 #include "directional-element-interface.hh"
13 #include "engraver.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 "stream-event.hh"
29 #include "translator.icc"
34 * direction of text-dynamic-event if not equal to direction of
37 - TODO: this engraver is too complicated. We should split it into
38 the handling of the basic grobs and the linespanner
40 - TODO: the line-spanner is not killed after the (de)crescs are
45 print text & hairpin dynamics.
47 class Dynamic_engraver : public Engraver
50 Spanner *line_spanner_;
53 Spanner *finished_line_spanner_;
54 Spanner *finished_cresc_;
56 Stream_event *script_ev_;
57 Stream_event *current_cresc_ev_;
59 Drul_array<Stream_event *> accepted_spanevents_drul_;
61 vector<Note_column*> pending_columns_;
62 vector<Grob*> pending_elements_;
66 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
67 DECLARE_ACKNOWLEDGER (accidental);
68 DECLARE_ACKNOWLEDGER (script);
69 DECLARE_ACKNOWLEDGER (stem_tremolo);
70 DECLARE_ACKNOWLEDGER (note_column);
71 DECLARE_ACKNOWLEDGER (slur);
72 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
73 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
76 virtual void finalize ();
77 void stop_translation_timestep ();
78 void process_music ();
81 Dynamic_engraver::Dynamic_engraver ()
86 finished_line_spanner_ = 0;
87 current_cresc_ev_ = 0;
91 accepted_spanevents_drul_[START] = 0;
92 accepted_spanevents_drul_[STOP] = 0;
95 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
97 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
100 TODO: probably broken.
102 ASSIGN_EVENT_ONCE (script_ev_, ev);
105 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
107 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
109 Direction d = to_dir (ev->get_property ("span-direction"));
112 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
114 /* Cancel any ongoing crescendo, either explicitly by \! or
115 implicitly by a new crescendo. Also avoid warning if cresc is
116 cancelled both implicitly and explicitly. */
117 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
118 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
122 Dynamic_engraver::process_music ()
124 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
128 Stream_event *rq = accepted_spanevents_drul_[START];
129 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
136 During a (de)crescendo, pending event will not be cleared,
137 and a line-spanner will always be created, as \< \! are already
140 Note: line-spanner must always have at least same duration
141 as (de)crecsendo, b.o. line-breaking.
145 maybe we should leave dynamic texts to the text-engraver and
146 simply acknowledge them?
150 script_ = make_item ("DynamicText", script_ev_->self_scm ());
151 script_->set_property ("text",
152 script_ev_->get_property ("text"));
154 if (Direction d = to_dir (script_ev_->get_property ("direction")))
155 set_grob_direction (line_spanner_, d);
156 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
157 set_grob_direction (script_, d);
159 Axis_group_interface::add_element (line_spanner_, script_);
162 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
163 ? accepted_spanevents_drul_[STOP] : script_ev_;
165 if (accepted_spanevents_drul_[STOP] || script_ev_)
168 finish side position alignment if the (de)cresc ends here, and
169 there are no new dynamics.
174 assert (!finished_cresc_ && cresc_);
178 cresc_->set_bound (RIGHT, script_);
179 add_bound_item (line_spanner_, script_);
182 finished_cresc_ = cresc_;
184 current_cresc_ev_ = 0;
186 else if (accepted_spanevents_drul_[STOP])
188 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
193 if (accepted_spanevents_drul_[START])
195 if (current_cresc_ev_)
197 string msg = _ ("already have a decrescendo");
198 if (current_cresc_ev_->in_event_class ("crescendo-event"))
199 msg = _ ("already have a crescendo");
201 accepted_spanevents_drul_[START]->origin ()->warning (msg);
202 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
206 current_cresc_ev_ = accepted_spanevents_drul_[START];
208 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
209 set_grob_direction (line_spanner_, d);
215 SCM start_sym = current_cresc_ev_->get_property ("class");
218 if (start_sym == ly_symbol2scm ("decrescendo-event"))
219 start_type = "decrescendo";
220 else if (start_sym == ly_symbol2scm ("crescendo-event"))
221 start_type = "crescendo";
224 programming_error ("unknown dynamic spanner type");
229 UGH. TODO: should read from original event, so appearance
230 may be altered with \tweak.
232 SCM s = get_property ((start_type + "Spanner").c_str ());
233 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
235 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
238 Pointer_group_interface::add_grob (finished_cresc_,
239 ly_symbol2scm ("adjacent-hairpins"),
242 Pointer_group_interface::add_grob (cresc_,
243 ly_symbol2scm ("adjacent-hairpins"),
249 This is a convenient (and legacy) interface to TextSpanners
250 for use in (de)crescendi.
255 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
256 cresc_->set_property ("style", s);
257 context ()->set_property ((start_type
258 + "Spanner").c_str (), SCM_EOL);
259 s = get_property ((start_type + "Text").c_str ());
261 FIXME: use get_markup () to check type.
263 if (scm_is_string (s) || scm_is_pair (s))
265 cresc_->set_property ("edge-text",
266 scm_cons (s, scm_makfrom0str ("")));
267 context ()->set_property ((start_type + "Text").c_str (),
274 cresc_->set_bound (LEFT, script_);
275 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
278 Axis_group_interface::add_element (line_spanner_, cresc_);
284 Dynamic_engraver::stop_translation_timestep ()
286 if (!current_cresc_ev_ && line_spanner_)
288 assert (!finished_line_spanner_);
289 finished_line_spanner_ = line_spanner_;
295 if (cresc_ && !cresc_->get_bound (LEFT))
297 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
298 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
302 accepted_spanevents_drul_[START] = 0;
303 accepted_spanevents_drul_[STOP] = 0;
307 Dynamic_engraver::finalize ()
312 && !line_spanner_->is_live ())
316 finished_line_spanner_ = line_spanner_;
321 && !cresc_->is_live ())
325 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
332 Dynamic_engraver::typeset_all ()
336 bool use_bar = to_boolean (get_property ("hairpinToBarline"))
337 && scm_is_string (get_property ("whichBar"))
341 if (!finished_cresc_->get_bound (RIGHT)
344 Grob *column_bound = unsmob_grob (use_bar
345 ? get_property ("currentCommandColumn")
346 : get_property ("currentMusicalColumn"));
348 finished_cresc_->set_bound (RIGHT, script_
352 if (finished_line_spanner_)
353 add_bound_item (finished_line_spanner_,
354 finished_cresc_->get_bound (RIGHT));
360 if (finished_line_spanner_)
365 extend-spanner-over-elements (finished_line_spanner_);
367 but this is rather kludgy, since finished_line_spanner_
368 typically has a staff-symbol field set , extending it over the
373 Grob *l = finished_line_spanner_->get_bound (LEFT);
374 Grob *r = finished_line_spanner_->get_bound (RIGHT);
376 finished_line_spanner_->set_bound (RIGHT, l);
378 finished_line_spanner_->set_bound (LEFT, r);
382 This is a isolated dynamic apparently, and does not even have
383 any interesting support item.
385 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
386 Item *ci = dynamic_cast<Item *> (cc);
387 finished_line_spanner_->set_bound (RIGHT, ci);
388 finished_line_spanner_->set_bound (LEFT, ci);
391 finished_line_spanner_ = 0;
397 Dynamic_engraver::acknowledge_accidental (Grob_info info)
400 Side_position_interface::add_support (line_spanner_, info.grob ());
405 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
408 Side_position_interface::add_support (line_spanner_, info.grob ());
413 Dynamic_engraver::acknowledge_slur (Grob_info info)
416 Side_position_interface::add_support (line_spanner_, info.grob ());
421 Dynamic_engraver::acknowledge_note_column (Grob_info info)
427 /* Don't refill killed spanner */
428 && line_spanner_->is_live ())
430 Side_position_interface::add_support (line_spanner_, info.grob ());
431 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
434 if (script_ && !script_->get_parent (X_AXIS))
436 extract_grob_set (info.grob (), "note-heads", heads);
439 Grob *head = heads[0];
440 script_->set_parent (head, X_AXIS);
441 Self_alignment_interface::set_center_parent (script_, X_AXIS);
447 if (!cresc_->get_bound (LEFT))
449 cresc_->set_bound (LEFT, info.grob ());
450 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
454 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
455 finished_cresc_->set_bound (RIGHT, info.grob ());
459 Dynamic_engraver::acknowledge_script (Grob_info info)
461 if (!line_spanner_ || !script_)
464 SCM p = info.grob ()->get_property ("script-priority");
469 DynamicText doesn't really have a script-priority field.
471 if (scm_is_number (p)
473 < scm_to_int (script_->get_property ("script-priority")))
474 Side_position_interface::add_support (line_spanner_, info.grob ());
477 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
478 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
479 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
480 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
481 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
483 ADD_TRANSLATOR (Dynamic_engraver,
485 "This engraver creates hairpins, dynamic texts, and their vertical\n"
486 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
487 "which takes care of vertical positioning. ",
490 "DynamicLineSpanner "