2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "axis-group-interface.hh"
22 #include "engraver.hh"
23 #include "dimensions.hh"
24 #include "directional-element-interface.hh"
25 #include "engraver.hh"
27 #include "international.hh"
28 #include "interval.hh"
29 #include "note-column.hh"
30 #include "paper-column.hh"
31 #include "pointer-group-interface.hh"
32 #include "script-interface.hh"
33 #include "self-alignment-interface.hh"
34 #include "side-position-interface.hh"
35 #include "staff-symbol-referencer.hh"
36 #include "stream-event.hh"
39 #include "text-interface.hh"
41 #include "translator.icc"
46 * direction of text-dynamic-event if not equal to direction of
49 - TODO: this engraver is too complicated. We should split it into
50 the handling of the basic grobs and the linespanner
52 - TODO: the line-spanner is not killed after the (de)crescs are
57 print text & hairpin dynamics.
59 class Dynamic_engraver : public Engraver
62 Spanner *line_spanner_;
65 Spanner *finished_line_spanner_;
66 Spanner *finished_cresc_;
68 Stream_event *script_ev_;
69 Stream_event *current_cresc_ev_;
71 Drul_array<Stream_event *> accepted_spanevents_drul_;
73 vector<Note_column *> pending_columns_;
74 vector<Grob *> pending_elements_;
78 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
79 DECLARE_ACKNOWLEDGER (note_column);
80 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
81 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
84 virtual void finalize ();
85 void stop_translation_timestep ();
86 void process_music ();
89 Dynamic_engraver::Dynamic_engraver ()
94 finished_line_spanner_ = 0;
95 current_cresc_ev_ = 0;
99 accepted_spanevents_drul_[START] = 0;
100 accepted_spanevents_drul_[STOP] = 0;
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
105 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
108 TODO: probably broken.
110 ASSIGN_EVENT_ONCE (script_ev_, ev);
113 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
115 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
117 Direction d = to_dir (ev->get_property ("span-direction"));
120 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
122 /* Cancel any ongoing crescendo, either explicitly by \! or
123 implicitly by a new crescendo. Also avoid warning if cresc is
124 canceled both implicitly and explicitly. */
125 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
126 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
130 Dynamic_engraver::process_music ()
132 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
136 Stream_event *rq = accepted_spanevents_drul_[START];
137 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
144 During a (de)crescendo, pending event will not be cleared,
145 and a line-spanner will always be created, as \< \! are already
148 Note: line-spanner must always have at least same duration
149 as (de)crecsendo, b.o. line-breaking.
153 maybe we should leave dynamic texts to the text-engraver and
154 simply acknowledge them?
158 script_ = make_item ("DynamicText", script_ev_->self_scm ());
159 script_->set_property ("text",
160 script_ev_->get_property ("text"));
162 if (Direction d = to_dir (script_ev_->get_property ("direction")))
163 set_grob_direction (line_spanner_, d);
164 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
165 set_grob_direction (script_, d);
167 Axis_group_interface::add_element (line_spanner_, script_);
170 if (accepted_spanevents_drul_[STOP] || script_ev_)
173 finish side position alignment if the (de)cresc ends here, and
174 there are no new dynamics.
179 assert (!finished_cresc_ && cresc_);
183 cresc_->set_bound (RIGHT, script_);
184 add_bound_item (line_spanner_, script_);
187 finished_cresc_ = cresc_;
188 announce_end_grob (finished_cresc_, SCM_EOL);
190 current_cresc_ev_ = 0;
192 else if (accepted_spanevents_drul_[STOP])
193 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
196 if (accepted_spanevents_drul_[START])
198 if (current_cresc_ev_)
200 string msg = _ ("already have a decrescendo");
201 if (current_cresc_ev_->in_event_class ("crescendo-event"))
202 msg = _ ("already have a crescendo");
204 accepted_spanevents_drul_[START]->origin ()->warning (msg);
205 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
209 current_cresc_ev_ = accepted_spanevents_drul_[START];
211 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
212 set_grob_direction (line_spanner_, d);
218 SCM start_sym = scm_car (current_cresc_ev_->get_property ("class"));
221 if (start_sym == ly_symbol2scm ("decrescendo-event"))
222 start_type = "decrescendo";
223 else if (start_sym == ly_symbol2scm ("crescendo-event"))
224 start_type = "crescendo";
227 programming_error ("unknown dynamic spanner type");
232 UGH. TODO: should read from original event, so appearance
233 may be altered with \tweak.
235 SCM s = get_property ((start_type + "Spanner").c_str ());
236 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
238 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
241 Pointer_group_interface::add_grob (finished_cresc_,
242 ly_symbol2scm ("adjacent-hairpins"),
245 Pointer_group_interface::add_grob (cresc_,
246 ly_symbol2scm ("adjacent-hairpins"),
252 This is a convenient (and legacy) interface to TextSpanners
253 for use in (de)crescendi.
258 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
259 cresc_->set_property ("style", s);
260 context ()->set_property ((start_type
261 + "Spanner").c_str (), SCM_EOL);
262 s = get_property ((start_type + "Text").c_str ());
263 if (Text_interface::is_markup (s))
265 cresc_->set_property ("text", s);
266 context ()->set_property ((start_type + "Text").c_str (),
272 set_nested_property (cresc_,
273 scm_list_3 (ly_symbol2scm ("bound-details"),
274 ly_symbol2scm ("left"),
275 ly_symbol2scm ("attach-dir")
277 scm_from_int (RIGHT));
283 cresc_->set_bound (LEFT, script_);
284 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
286 Axis_group_interface::add_element (line_spanner_, cresc_);
292 Dynamic_engraver::stop_translation_timestep ()
294 if (!current_cresc_ev_ && line_spanner_)
296 assert (!finished_line_spanner_);
297 finished_line_spanner_ = line_spanner_;
303 if (cresc_ && !cresc_->get_bound (LEFT))
305 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
306 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
310 accepted_spanevents_drul_[START] = 0;
311 accepted_spanevents_drul_[STOP] = 0;
315 Dynamic_engraver::finalize ()
320 && !line_spanner_->is_live ())
324 finished_line_spanner_ = line_spanner_;
329 && !cresc_->is_live ())
333 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
340 Dynamic_engraver::typeset_all ()
344 if (!finished_cresc_->get_bound (RIGHT))
347 Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
349 finished_cresc_->set_bound (RIGHT, script_
353 if (finished_line_spanner_)
354 add_bound_item (finished_line_spanner_,
355 finished_cresc_->get_bound (RIGHT));
361 if (finished_line_spanner_)
366 extend-spanner-over-elements (finished_line_spanner_);
368 but this is rather kludgy, since finished_line_spanner_
369 typically has a staff-symbol field set , extending it over the
374 Grob *l = finished_line_spanner_->get_bound (LEFT);
375 Grob *r = finished_line_spanner_->get_bound (RIGHT);
377 finished_line_spanner_->set_bound (RIGHT, l);
379 finished_line_spanner_->set_bound (LEFT, r);
383 This is a isolated dynamic apparently, and does not even have
384 any interesting support item.
386 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
387 Item *ci = dynamic_cast<Item *> (cc);
388 finished_line_spanner_->set_bound (RIGHT, ci);
389 finished_line_spanner_->set_bound (LEFT, ci);
391 finished_line_spanner_ = 0;
396 Dynamic_engraver::acknowledge_note_column (Grob_info info)
402 /* Don't refill killed spanner */
403 && line_spanner_->is_live ())
405 Side_position_interface::add_support (line_spanner_, info.grob ());
406 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
409 if (script_ && !script_->get_parent (X_AXIS))
411 extract_grob_set (info.grob (), "note-heads", heads);
414 Grob *head = heads[0];
415 script_->set_parent (head, X_AXIS);
416 Self_alignment_interface::set_center_parent (script_, X_AXIS);
422 if (!cresc_->get_bound (LEFT))
424 cresc_->set_bound (LEFT, info.grob ());
425 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
429 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
430 finished_cresc_->set_bound (RIGHT, info.grob ());
433 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
435 ADD_TRANSLATOR (Dynamic_engraver,
437 "Create hairpins, dynamic texts, and their vertical"
438 " alignments. The symbols are collected onto a"
439 " @code{DynamicLineSpanner} grob which takes care of vertical"
443 "DynamicLineSpanner "
444 "DynamicTextSpanner "