Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / dynamic-engraver.cc
blob14522c6376190c9ca84817d96af2a41b585d2844
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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"
21 #include "context.hh"
22 #include "engraver.hh"
23 #include "dimensions.hh"
24 #include "directional-element-interface.hh"
25 #include "engraver.hh"
26 #include "hairpin.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"
37 #include "warn.hh"
38 #include "spanner.hh"
39 #include "text-interface.hh"
41 #include "translator.icc"
44 TODO:
46 * direction of text-dynamic-event if not equal to direction of
47 line-spanner
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
53 finished.
56 /**
57 print text & hairpin dynamics.
59 class Dynamic_engraver : public Engraver
61 Item *script_;
62 Spanner *line_spanner_;
63 Spanner *cresc_;
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_;
76 void typeset_all ();
78 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
79 DECLARE_ACKNOWLEDGER (note_column);
80 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
81 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
83 protected:
84 virtual void finalize ();
85 void stop_translation_timestep ();
86 void process_music ();
89 Dynamic_engraver::Dynamic_engraver ()
91 script_ = 0;
92 finished_cresc_ = 0;
93 line_spanner_ = 0;
94 finished_line_spanner_ = 0;
95 current_cresc_ev_ = 0;
96 cresc_ = 0;
98 script_ev_ = 0;
99 accepted_spanevents_drul_[START] = 0;
100 accepted_spanevents_drul_[STOP] = 0;
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
104 void
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);
114 void
115 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
117 Direction d = to_dir (ev->get_property ("span-direction"));
119 if (d == START)
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 cancelled both implicitly and explicitly. */
125 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
126 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
129 void
130 Dynamic_engraver::process_music ()
132 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
134 if (!line_spanner_)
136 Stream_event *rq = accepted_spanevents_drul_[START];
137 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
138 if (script_ev_)
139 rq = script_ev_;
144 During a (de)crescendo, pending event will not be cleared,
145 and a line-spanner will always be created, as \< \! are already
146 two events.
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?
156 if (script_ev_)
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 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
171 ? accepted_spanevents_drul_[STOP] : script_ev_;
173 if (accepted_spanevents_drul_[STOP] || script_ev_)
176 finish side position alignment if the (de)cresc ends here, and
177 there are no new dynamics.
180 if (cresc_)
182 assert (!finished_cresc_ && cresc_);
184 if (script_)
186 cresc_->set_bound (RIGHT, script_);
187 add_bound_item (line_spanner_, script_);
190 finished_cresc_ = cresc_;
191 announce_end_grob (finished_cresc_, SCM_EOL);
192 cresc_ = 0;
193 current_cresc_ev_ = 0;
195 else if (accepted_spanevents_drul_[STOP])
197 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
198 stop_ev = 0;
202 if (accepted_spanevents_drul_[START])
204 if (current_cresc_ev_)
206 string msg = _ ("already have a decrescendo");
207 if (current_cresc_ev_->in_event_class ("crescendo-event"))
208 msg = _ ("already have a crescendo");
210 accepted_spanevents_drul_[START]->origin ()->warning (msg);
211 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
213 else
215 current_cresc_ev_ = accepted_spanevents_drul_[START];
217 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
218 set_grob_direction (line_spanner_, d);
221 TODO: Use symbols.
224 SCM start_sym = current_cresc_ev_->get_property ("class");
225 string start_type;
227 if (start_sym == ly_symbol2scm ("decrescendo-event"))
228 start_type = "decrescendo";
229 else if (start_sym == ly_symbol2scm ("crescendo-event"))
230 start_type = "crescendo";
231 else
233 programming_error ("unknown dynamic spanner type");
234 return;
238 UGH. TODO: should read from original event, so appearance
239 may be altered with \tweak.
241 SCM s = get_property ((start_type + "Spanner").c_str ());
242 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
244 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
245 if (finished_cresc_)
247 Pointer_group_interface::add_grob (finished_cresc_,
248 ly_symbol2scm ("adjacent-hairpins"),
249 cresc_);
251 Pointer_group_interface::add_grob (cresc_,
252 ly_symbol2scm ("adjacent-hairpins"),
253 finished_cresc_);
258 This is a convenient (and legacy) interface to TextSpanners
259 for use in (de)crescendi.
260 Hmm.
262 else
264 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
265 cresc_->set_property ("style", s);
266 context ()->set_property ((start_type
267 + "Spanner").c_str (), SCM_EOL);
268 s = get_property ((start_type + "Text").c_str ());
269 if (Text_interface::is_markup (s))
271 cresc_->set_property ("text", s);
272 context ()->set_property ((start_type + "Text").c_str (),
273 SCM_EOL);
276 if (script_)
278 set_nested_property (cresc_,
279 scm_list_3 (ly_symbol2scm ("bound-details"),
280 ly_symbol2scm ("left"),
281 ly_symbol2scm ("attach-dir")
283 scm_from_int (RIGHT));
287 if (script_)
289 cresc_->set_bound (LEFT, script_);
290 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
292 Axis_group_interface::add_element (line_spanner_, cresc_);
297 void
298 Dynamic_engraver::stop_translation_timestep ()
300 if (!current_cresc_ev_ && line_spanner_)
302 assert (!finished_line_spanner_);
303 finished_line_spanner_ = line_spanner_;
304 line_spanner_ = 0;
307 typeset_all ();
309 if (cresc_ && !cresc_->get_bound (LEFT))
311 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
312 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
315 script_ev_ = 0;
316 accepted_spanevents_drul_[START] = 0;
317 accepted_spanevents_drul_[STOP] = 0;
320 void
321 Dynamic_engraver::finalize ()
323 typeset_all ();
325 if (line_spanner_
326 && !line_spanner_->is_live ())
327 line_spanner_ = 0;
328 if (line_spanner_)
330 finished_line_spanner_ = line_spanner_;
331 typeset_all ();
334 if (cresc_
335 && !cresc_->is_live ())
336 cresc_ = 0;
337 if (cresc_)
339 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
340 cresc_->suicide ();
341 cresc_ = 0;
345 void
346 Dynamic_engraver::typeset_all ()
348 if (finished_cresc_)
350 if (!finished_cresc_->get_bound (RIGHT))
353 Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
355 finished_cresc_->set_bound (RIGHT, script_
356 ? script_
357 : column_bound);
359 if (finished_line_spanner_)
360 add_bound_item (finished_line_spanner_,
361 finished_cresc_->get_bound (RIGHT));
363 finished_cresc_ = 0;
366 script_ = 0;
367 if (finished_line_spanner_)
370 We used to have
372 extend-spanner-over-elements (finished_line_spanner_);
374 but this is rather kludgy, since finished_line_spanner_
375 typically has a staff-symbol field set , extending it over the
376 entire staff.
380 Grob *l = finished_line_spanner_->get_bound (LEFT);
381 Grob *r = finished_line_spanner_->get_bound (RIGHT);
382 if (!r && l)
383 finished_line_spanner_->set_bound (RIGHT, l);
384 else if (!l && r)
385 finished_line_spanner_->set_bound (LEFT, r);
386 else if (!r && !l)
389 This is a isolated dynamic apparently, and does not even have
390 any interesting support item.
392 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
393 Item *ci = dynamic_cast<Item *> (cc);
394 finished_line_spanner_->set_bound (RIGHT, ci);
395 finished_line_spanner_->set_bound (LEFT, ci);
397 finished_line_spanner_ = 0;
401 void
402 Dynamic_engraver::acknowledge_note_column (Grob_info info)
404 if (!line_spanner_)
405 return;
407 if (line_spanner_
408 /* Don't refill killed spanner */
409 && line_spanner_->is_live ())
411 Side_position_interface::add_support (line_spanner_, info.grob ());
412 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
415 if (script_ && !script_->get_parent (X_AXIS))
417 extract_grob_set (info.grob (), "note-heads", heads);
418 if (heads.size ())
420 Grob *head = heads[0];
421 script_->set_parent (head, X_AXIS);
422 Self_alignment_interface::set_center_parent (script_, X_AXIS);
426 if (cresc_)
428 if (!cresc_->get_bound (LEFT))
430 cresc_->set_bound (LEFT, info.grob ());
431 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
435 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
436 finished_cresc_->set_bound (RIGHT, info.grob ());
439 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
441 ADD_TRANSLATOR (Dynamic_engraver,
442 /* doc */
443 "Create hairpins, dynamic texts, and their vertical"
444 " alignments. The symbols are collected onto a"
445 " @code{DynamicLineSpanner} grob which takes care of vertical"
446 " positioning.",
448 /* create */
449 "DynamicLineSpanner "
450 "DynamicTextSpanner "
451 "DynamicText "
452 "Hairpin ",
454 /* read */
457 /* write */