Fixes Issue 1504, allowing feather beam line breaking.
[lilypond/patrick.git] / lily / new-dynamic-engraver.cc
blobe225d97f1fd1bf4285988c27700839ebb3bca595
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2008--2011 Han-Wen Nienhuys <hanwen@lilypond.org>
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 "engraver.hh"
21 #include "hairpin.hh"
22 #include "international.hh"
23 #include "item.hh"
24 #include "note-column.hh"
25 #include "pointer-group-interface.hh"
26 #include "self-alignment-interface.hh"
27 #include "spanner.hh"
28 #include "stream-event.hh"
29 #include "text-interface.hh"
31 #include "translator.icc"
33 class New_dynamic_engraver : public Engraver
35 TRANSLATOR_DECLARATIONS (New_dynamic_engraver);
36 DECLARE_ACKNOWLEDGER (note_column);
37 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
38 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
40 protected:
41 virtual void process_music ();
42 virtual void stop_translation_timestep ();
43 virtual void finalize ();
45 private:
46 SCM get_property_setting (Stream_event *evt, char const *evprop,
47 char const *ctxprop);
48 string get_spanner_type (Stream_event *ev);
50 Drul_array<Stream_event *> accepted_spanevents_drul_;
51 Spanner *current_spanner_;
52 Spanner *finished_spanner_;
54 Item *script_;
55 Stream_event *script_event_;
56 Stream_event *current_span_event_;
59 New_dynamic_engraver::New_dynamic_engraver ()
61 script_event_ = 0;
62 current_span_event_ = 0;
63 script_ = 0;
64 finished_spanner_ = 0;
65 current_spanner_ = 0;
66 accepted_spanevents_drul_.set (0, 0);
69 IMPLEMENT_TRANSLATOR_LISTENER (New_dynamic_engraver, absolute_dynamic);
70 void
71 New_dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
73 ASSIGN_EVENT_ONCE (script_event_, ev);
76 IMPLEMENT_TRANSLATOR_LISTENER (New_dynamic_engraver, span_dynamic);
77 void
78 New_dynamic_engraver::listen_span_dynamic (Stream_event *ev)
80 Direction d = to_dir (ev->get_property ("span-direction"));
82 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
85 SCM
86 New_dynamic_engraver::get_property_setting (Stream_event *evt,
87 char const *evprop,
88 char const *ctxprop)
90 SCM spanner_type = evt->get_property (evprop);
91 if (spanner_type == SCM_EOL)
92 spanner_type = get_property (ctxprop);
93 return spanner_type;
96 void
97 New_dynamic_engraver::process_music ()
99 if (current_spanner_
100 && (accepted_spanevents_drul_[STOP]
101 || script_event_
102 || accepted_spanevents_drul_[START]))
104 Stream_event *ender = accepted_spanevents_drul_[STOP];
105 if (!ender)
106 ender = script_event_;
108 if (!ender)
109 ender = accepted_spanevents_drul_[START];
111 finished_spanner_ = current_spanner_;
112 announce_end_grob (finished_spanner_, ender->self_scm ());
113 current_spanner_ = 0;
114 current_span_event_ = 0;
117 if (accepted_spanevents_drul_[START])
119 current_span_event_ = accepted_spanevents_drul_[START];
121 string start_type = get_spanner_type (current_span_event_);
122 SCM cresc_type = get_property_setting (current_span_event_, "span-type",
123 (start_type + "Spanner").c_str ());
125 if (cresc_type == ly_symbol2scm ("text"))
127 current_spanner_
128 = make_spanner ("DynamicTextSpanner",
129 accepted_spanevents_drul_[START]->self_scm ());
131 SCM text = get_property_setting (current_span_event_, "span-text",
132 (start_type + "Text").c_str ());
133 if (Text_interface::is_markup (text))
134 current_spanner_->set_property ("text", text);
136 else
138 if (cresc_type != ly_symbol2scm ("hairpin"))
140 string as_string = ly_scm_write_string (cresc_type);
141 current_span_event_
142 ->origin()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str()));
144 current_spanner_ = make_spanner ("Hairpin",
145 current_span_event_->self_scm ());
147 if (finished_spanner_)
149 if (Hairpin::has_interface (finished_spanner_))
150 Pointer_group_interface::add_grob (finished_spanner_,
151 ly_symbol2scm ("adjacent-spanners"),
152 current_spanner_);
153 if (Hairpin::has_interface (current_spanner_))
154 Pointer_group_interface::add_grob (current_spanner_,
155 ly_symbol2scm ("adjacent-spanners"),
156 finished_spanner_);
160 if (script_event_)
162 script_ = make_item ("DynamicText", script_event_->self_scm ());
163 script_->set_property ("text",
164 script_event_->get_property ("text"));
166 if (finished_spanner_)
167 finished_spanner_->set_bound (RIGHT, script_);
168 if (current_spanner_)
169 current_spanner_->set_bound (LEFT, script_);
173 void
174 New_dynamic_engraver::stop_translation_timestep ()
176 if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
177 finished_spanner_
178 ->set_bound (RIGHT,
179 unsmob_grob (get_property ("currentMusicalColumn")));
181 if (current_spanner_ && !current_spanner_->get_bound (LEFT))
182 current_spanner_
183 ->set_bound (LEFT,
184 unsmob_grob (get_property ("currentMusicalColumn")));
185 script_ = 0;
186 script_event_ = 0;
187 accepted_spanevents_drul_.set (0, 0);
188 finished_spanner_ = 0;
191 void
192 New_dynamic_engraver::finalize ()
194 if (current_spanner_
195 && !current_spanner_->is_live ())
196 current_spanner_ = 0;
197 if (current_spanner_)
199 current_span_event_
200 ->origin ()->warning (_f ("unterminated %s",
201 get_spanner_type (current_span_event_)
202 .c_str ()));
203 current_spanner_->suicide ();
204 current_spanner_ = 0;
208 string
209 New_dynamic_engraver::get_spanner_type (Stream_event *ev)
211 string type;
212 SCM start_sym = ev->get_property ("class");
214 if (start_sym == ly_symbol2scm ("decrescendo-event"))
215 type = "decrescendo";
216 else if (start_sym == ly_symbol2scm ("crescendo-event"))
217 type = "crescendo";
218 else
219 programming_error ("unknown dynamic spanner type");
221 return type;
224 void
225 New_dynamic_engraver::acknowledge_note_column (Grob_info info)
227 if (script_ && !script_->get_parent (X_AXIS))
229 extract_grob_set (info.grob (), "note-heads", heads);
231 Spacing constraints may require dynamics to be aligned on rests,
232 so check for a rest if this note column has no note heads.
234 Grob *x_parent = (heads.size ()
235 ? heads[0]
236 : unsmob_grob (info.grob ()->get_object ("rest")));
237 if (x_parent)
239 script_->set_parent (x_parent, X_AXIS);
240 Self_alignment_interface::set_center_parent (script_, X_AXIS);
244 if (current_spanner_ && !current_spanner_->get_bound (LEFT))
245 current_spanner_->set_bound (LEFT, info.grob ());
246 if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
247 finished_spanner_->set_bound (RIGHT, info.grob ());
250 ADD_ACKNOWLEDGER (New_dynamic_engraver, note_column);
251 ADD_TRANSLATOR (New_dynamic_engraver,
252 /* doc */
253 "Create hairpins, dynamic texts and dynamic text spanners.",
255 /* create */
256 "DynamicTextSpanner "
257 "DynamicText "
258 "Hairpin ",
260 /* read */
261 "crescendoSpanner "
262 "crescendoText "
263 "currentMusicalColumn "
264 "decrescendoSpanner "
265 "decrescendoText ",
267 /* write */