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 "engraver.hh"
23 #include "directional-element-interface.hh"
24 #include "international.hh"
25 #include "note-column.hh"
28 #include "stream-event.hh"
31 #include "translator.icc"
36 This is largely similar to Phrasing_slur_engraver. Check if fixes
39 (on principle, engravers don't use inheritance for code sharing)
44 It is possible that a slur starts and ends on the same note. At
45 least, it is for phrasing slurs: a note can be both beginning and
48 class Slur_engraver : public Engraver
50 vector<Stream_event *> start_events_;
51 vector<Stream_event *> stop_events_;
52 vector<Grob *> slurs_;
53 vector<Grob *> end_slurs_;
54 vector<Grob_info> objects_to_acknowledge_;
56 void set_melisma (bool);
59 DECLARE_TRANSLATOR_LISTENER (slur);
60 DECLARE_ACKNOWLEDGER (inline_accidental);
61 DECLARE_ACKNOWLEDGER (fingering);
62 DECLARE_ACKNOWLEDGER (note_column);
63 DECLARE_ACKNOWLEDGER (script);
64 DECLARE_ACKNOWLEDGER (dots);
65 DECLARE_ACKNOWLEDGER (text_script);
66 DECLARE_ACKNOWLEDGER (tie);
67 DECLARE_ACKNOWLEDGER (tuplet_number);
69 void acknowledge_extra_object (Grob_info);
70 void stop_translation_timestep ();
71 void process_music ();
73 virtual void finalize ();
76 TRANSLATOR_DECLARATIONS (Slur_engraver);
79 Slur_engraver::Slur_engraver ()
83 IMPLEMENT_TRANSLATOR_LISTENER (Slur_engraver, slur);
85 Slur_engraver::listen_slur (Stream_event *ev)
87 Direction d = to_dir (ev->get_property ("span-direction"));
89 start_events_.push_back (ev);
91 stop_events_.push_back (ev);
92 else ev->origin ()->warning (_f ("direction of %s invalid: %d",
93 "slur-event", int (d)));
97 Slur_engraver::set_melisma (bool m)
99 context ()->set_property ("slurMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
103 Slur_engraver::acknowledge_note_column (Grob_info info)
105 Grob *e = info.grob ();
106 for (vsize i = slurs_.size (); i--;)
107 Slur::add_column (slurs_[i], e);
108 for (vsize i = end_slurs_.size (); i--;)
109 Slur::add_column (end_slurs_[i], e);
113 Slur_engraver::acknowledge_extra_object (Grob_info info)
115 objects_to_acknowledge_.push_back (info);
119 Slur_engraver::acknowledge_inline_accidental (Grob_info info)
121 acknowledge_extra_object (info);
125 Slur_engraver::acknowledge_dots (Grob_info info)
127 acknowledge_extra_object (info);
131 Slur_engraver::acknowledge_fingering (Grob_info info)
133 acknowledge_extra_object (info);
137 Slur_engraver::acknowledge_tuplet_number (Grob_info info)
139 acknowledge_extra_object (info);
143 Slur_engraver::acknowledge_script (Grob_info info)
145 if (!info.grob ()->internal_has_interface (ly_symbol2scm ("dynamic-interface")))
146 acknowledge_extra_object (info);
150 Slur_engraver::acknowledge_text_script (Grob_info info)
152 acknowledge_extra_object (info);
156 Slur_engraver::acknowledge_tie (Grob_info info)
158 acknowledge_extra_object (info);
162 Slur_engraver::finalize ()
164 for (vsize i = 0; i < slurs_.size (); i++)
166 slurs_[i]->warning (_ ("unterminated slur"));
167 slurs_[i]->suicide ();
172 Slur_engraver::process_music ()
174 for (vsize i = 0; i < stop_events_.size (); i++)
176 Stream_event *ev = stop_events_[i];
177 string id = robust_scm2string (ev->get_property ("spanner-id"), "");
179 // Find the slurs that are ended with this event (by checking the spanner-id)
181 for (vsize j = slurs_.size (); j--;)
183 if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
186 end_slurs_.push_back (slurs_[j]);
187 slurs_.erase (slurs_.begin () + j);
192 // Ignore redundant stop events for this id
193 for (vsize j = stop_events_.size (); --j > i;)
195 if (id == robust_scm2string (stop_events_[j]->get_property ("spanner-id"), ""))
196 stop_events_.erase (stop_events_.begin() + j);
200 ev->origin ()->warning (_ ("cannot end slur"));
203 vsize old_slurs = slurs_.size ();
204 for (vsize i = start_events_.size (); i--;)
206 Stream_event *ev = start_events_[i];
207 string id = robust_scm2string (ev->get_property ("spanner-id"), "");
208 Direction updown = to_dir (ev->get_property ("direction"));
211 for (vsize j = 0; !(completed = (j == slurs_.size ())); j++)
213 // Check if we already have a slur with the same spanner-id.
214 if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
218 // We already have an old slur, so give a warning
219 // and completely ignore the new slur.
220 ev->origin ()->warning (_ ("already have slur"));
221 start_events_.erase (start_events_.begin () + i);
225 // If this slur event has no direction, it will not
226 // contribute anything new to the existing slur(s), so
227 // we can ignore it. This is not entirely accurate:
228 // tweaks or context properties like those set with
229 // \slurUp can still override a neutral direction, so
230 // when encountering a slur event with "opposite"
231 // direction first, then one with neutral direction, we
232 // only let the "opposite" direction remain, while if
233 // the order is the other way round, a double slur
234 // results since the direction of the first slur is no
235 // longer attributable to a "neutral" slur event. A
236 // mixture of neutral and directed events is nothing
237 // that the partcombiner should crank out, and it would
238 // be decidedly strange for manual input.
243 // If the existing slur does not have a direction yet,
246 Direction slur_dir = to_dir (slurs_[j]->get_property ("direction"));
250 set_grob_direction (slurs_[j], updown);
254 // If the existing slur has the same direction as ours, drop ours
256 if (slur_dir == updown)
260 // If the loop completed, our slur is new
263 Grob *slur = make_spanner ("Slur", ev->self_scm ());
264 slur->set_property ("spanner-id", ly_string2scm (id));
266 set_grob_direction (slur, updown);
267 slurs_.push_back (slur);
269 if (to_boolean (get_property ("doubleSlurs")))
271 set_grob_direction (slur, DOWN);
272 slur = make_spanner ("Slur", ev->self_scm ());
273 slur->set_property ("spanner-id", ly_string2scm (id));
274 set_grob_direction (slur, UP);
275 slurs_.push_back (slur);
279 set_melisma (slurs_.size ());
283 Slur_engraver::stop_translation_timestep ()
285 if (Grob *g = unsmob_grob (get_property ("currentCommandColumn")))
287 for (vsize i = 0; i < end_slurs_.size (); i++)
288 Slur::add_extra_encompass (end_slurs_[i], g);
290 if (!start_events_.size ())
291 for (vsize i = 0; i < slurs_.size (); i++)
292 Slur::add_extra_encompass (slurs_[i], g);
295 for (vsize i = 0; i < end_slurs_.size (); i++)
297 Spanner *s = dynamic_cast<Spanner *> (end_slurs_[i]);
298 if (!s->get_bound (RIGHT))
299 s->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
300 announce_end_grob (s, SCM_EOL);
303 for (vsize i = 0; i < objects_to_acknowledge_.size (); i++)
304 Slur::auxiliary_acknowledge_extra_object (objects_to_acknowledge_[i], slurs_, end_slurs_);
306 objects_to_acknowledge_.clear ();
308 start_events_.clear ();
309 stop_events_.clear ();
312 ADD_ACKNOWLEDGER (Slur_engraver, inline_accidental);
313 ADD_ACKNOWLEDGER (Slur_engraver, fingering);
314 ADD_ACKNOWLEDGER (Slur_engraver, note_column);
315 ADD_ACKNOWLEDGER (Slur_engraver, script);
316 ADD_ACKNOWLEDGER (Slur_engraver, text_script);
317 ADD_ACKNOWLEDGER (Slur_engraver, dots);
318 ADD_ACKNOWLEDGER (Slur_engraver, tie);
319 ADD_ACKNOWLEDGER (Slur_engraver, tuplet_number);
320 ADD_TRANSLATOR (Slur_engraver,
322 "Build slur grobs from slur events.",