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"
27 #include "translator.icc"
32 * direction of text-dynamic-event if not equal to direction of
35 - TODO: this engraver is too complicated. We should split it into
36 the handling of the basic grobs and the linespanner
38 - TODO: the line-spanner is not killed after the (de)crescs are
43 print text & hairpin dynamics.
45 class Dynamic_engraver : public Engraver
48 Spanner *line_spanner_;
51 Spanner *finished_line_spanner_;
52 Spanner *finished_cresc_;
54 Stream_event *script_ev_;
55 Stream_event *current_cresc_ev_;
57 Drul_array<Stream_event *> accepted_spanevents_drul_;
59 vector<Note_column*> pending_columns_;
60 vector<Grob*> pending_elements_;
64 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
65 DECLARE_ACKNOWLEDGER (accidental);
66 DECLARE_ACKNOWLEDGER (script);
67 DECLARE_ACKNOWLEDGER (stem_tremolo);
68 DECLARE_ACKNOWLEDGER (note_column);
69 DECLARE_ACKNOWLEDGER (slur);
70 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
71 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
74 virtual void finalize ();
75 void stop_translation_timestep ();
76 void process_music ();
79 Dynamic_engraver::Dynamic_engraver ()
84 finished_line_spanner_ = 0;
85 current_cresc_ev_ = 0;
89 accepted_spanevents_drul_[START] = 0;
90 accepted_spanevents_drul_[STOP] = 0;
93 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
95 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
98 TODO: probably broken.
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
105 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
107 Direction d = to_dir (ev->get_property ("span-direction"));
109 accepted_spanevents_drul_[d] = ev;
110 if (current_cresc_ev_ && d == START)
111 accepted_spanevents_drul_[STOP] = ev;
115 Dynamic_engraver::process_music ()
117 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
121 Stream_event *rq = accepted_spanevents_drul_[START];
122 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
129 During a (de)crescendo, pending event will not be cleared,
130 and a line-spanner will always be created, as \< \! are already
133 Note: line-spanner must always have at least same duration
134 as (de)crecsendo, b.o. line-breaking.
138 maybe we should leave dynamic texts to the text-engraver and
139 simply acknowledge them?
143 script_ = make_item ("DynamicText", script_ev_->self_scm ());
144 script_->set_property ("text",
145 script_ev_->get_property ("text"));
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);
152 Axis_group_interface::add_element (line_spanner_, script_);
155 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
156 ? accepted_spanevents_drul_[STOP] : script_ev_;
158 if (accepted_spanevents_drul_[STOP] || script_ev_)
161 finish side position alignment if the (de)cresc ends here, and
162 there are no new dynamics.
167 assert (!finished_cresc_ && cresc_);
171 cresc_->set_bound (RIGHT, script_);
172 add_bound_item (line_spanner_, script_);
175 finished_cresc_ = cresc_;
177 current_cresc_ev_ = 0;
179 else if (accepted_spanevents_drul_[STOP])
181 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
186 if (accepted_spanevents_drul_[START])
188 if (current_cresc_ev_)
190 string msg = _ ("already have a decrescendo");
191 if (current_cresc_ev_->in_event_class ("crescendo-event"))
192 msg = _ ("already have a crescendo");
194 accepted_spanevents_drul_[START]->origin ()->warning (msg);
195 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
199 current_cresc_ev_ = accepted_spanevents_drul_[START];
201 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
202 set_grob_direction (line_spanner_, d);
208 SCM start_sym = current_cresc_ev_->get_property ("class");
211 if (start_sym == ly_symbol2scm ("decrescendo-event"))
212 start_type = "decrescendo";
213 else if (start_sym == ly_symbol2scm ("crescendo-event"))
214 start_type = "crescendo";
217 programming_error ("unknown dynamic spanner type");
222 UGH. TODO: should read from original event, so appearance
223 may be altered with \tweak.
225 SCM s = get_property ((start_type + "Spanner").c_str ());
226 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
228 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
231 Pointer_group_interface::add_grob (finished_cresc_,
232 ly_symbol2scm ("adjacent-hairpins"),
235 Pointer_group_interface::add_grob (cresc_,
236 ly_symbol2scm ("adjacent-hairpins"),
239 cresc_->set_property ("grow-direction",
240 scm_from_int ((start_type == "crescendo")
241 ? BIGGER : SMALLER));
245 This is a convenient (and legacy) interface to TextSpanners
246 for use in (de)crescendi.
251 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
252 cresc_->set_property ("style", s);
253 context ()->set_property ((start_type
254 + "Spanner").c_str (), SCM_EOL);
255 s = get_property ((start_type + "Text").c_str ());
257 FIXME: use get_markup () to check type.
259 if (scm_is_string (s) || scm_is_pair (s))
261 cresc_->set_property ("edge-text",
262 scm_cons (s, scm_makfrom0str ("")));
263 context ()->set_property ((start_type + "Text").c_str (),
270 cresc_->set_bound (LEFT, script_);
271 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
274 Axis_group_interface::add_element (line_spanner_, cresc_);
280 Dynamic_engraver::stop_translation_timestep ()
282 if (!current_cresc_ev_ && line_spanner_)
284 assert (!finished_line_spanner_);
285 finished_line_spanner_ = line_spanner_;
291 if (cresc_ && !cresc_->get_bound (LEFT))
293 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
294 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
298 accepted_spanevents_drul_[START] = 0;
299 accepted_spanevents_drul_[STOP] = 0;
303 Dynamic_engraver::finalize ()
308 && !line_spanner_->is_live ())
312 finished_line_spanner_ = line_spanner_;
317 && !cresc_->is_live ())
321 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
328 Dynamic_engraver::typeset_all ()
332 bool use_bar = to_boolean (get_property ("hairpinToBarline"))
333 && scm_is_string (get_property ("whichBar"))
337 if (!finished_cresc_->get_bound (RIGHT)
340 Grob *column_bound = unsmob_grob (use_bar
341 ? get_property ("currentCommandColumn")
342 : get_property ("currentMusicalColumn"));
344 finished_cresc_->set_bound (RIGHT, script_
348 if (finished_line_spanner_)
349 add_bound_item (finished_line_spanner_,
350 finished_cresc_->get_bound (RIGHT));
356 if (finished_line_spanner_)
361 extend-spanner-over-elements (finished_line_spanner_);
363 but this is rather kludgy, since finished_line_spanner_
364 typically has a staff-symbol field set , extending it over the
369 Grob *l = finished_line_spanner_->get_bound (LEFT);
370 Grob *r = finished_line_spanner_->get_bound (RIGHT);
372 finished_line_spanner_->set_bound (RIGHT, l);
374 finished_line_spanner_->set_bound (LEFT, r);
378 This is a isolated dynamic apparently, and does not even have
379 any interesting support item.
381 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
382 Item *ci = dynamic_cast<Item *> (cc);
383 finished_line_spanner_->set_bound (RIGHT, ci);
384 finished_line_spanner_->set_bound (LEFT, ci);
387 finished_line_spanner_ = 0;
393 Dynamic_engraver::acknowledge_accidental (Grob_info info)
396 Side_position_interface::add_support (line_spanner_, info.grob ());
401 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
404 Side_position_interface::add_support (line_spanner_, info.grob ());
409 Dynamic_engraver::acknowledge_slur (Grob_info info)
412 Side_position_interface::add_support (line_spanner_, info.grob ());
417 Dynamic_engraver::acknowledge_note_column (Grob_info info)
423 /* Don't refill killed spanner */
424 && line_spanner_->is_live ())
426 Side_position_interface::add_support (line_spanner_, info.grob ());
427 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
430 if (script_ && !script_->get_parent (X_AXIS))
432 extract_grob_set (info.grob (), "note-heads", heads);
435 Grob *head = heads[0];
436 script_->set_parent (head, X_AXIS);
437 Self_alignment_interface::set_center_parent (script_, X_AXIS);
443 if (!cresc_->get_bound (LEFT))
445 cresc_->set_bound (LEFT, info.grob ());
446 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
450 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
451 finished_cresc_->set_bound (RIGHT, info.grob ());
455 Dynamic_engraver::acknowledge_script (Grob_info info)
457 if (!line_spanner_ || !script_)
460 SCM p = info.grob ()->get_property ("script-priority");
465 DynamicText doesn't really have a script-priority field.
467 if (scm_is_number (p)
469 < scm_to_int (script_->get_property ("script-priority")))
470 Side_position_interface::add_support (line_spanner_, info.grob ());
473 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
474 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
475 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
476 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
477 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
479 ADD_TRANSLATOR (Dynamic_engraver,
481 "This engraver creates hairpins, dynamic texts, and their vertical\n"
482 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
483 "which takes care of vertical positioning. ",
485 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
486 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",