]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-engraver.cc
e4962852888aa809b9875aea09109313f82105cd
[lilypond.git] / lily / slur-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "context.hh"
23 #include "directional-element-interface.hh"
24 #include "international.hh"
25 #include "note-column.hh"
26 #include "slur.hh"
27 #include "spanner.hh"
28 #include "stream-event.hh"
29 #include "warn.hh"
30
31 #include "translator.icc"
32
33 /*
34   NOTE NOTE NOTE
35
36   This is largely similar to Phrasing_slur_engraver. Check if fixes
37   apply there too.
38
39   (on principle, engravers don't use inheritance for code sharing)
40
41  */
42
43 /*
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
46   ending of a phrase.
47 */
48 class Slur_engraver : public Engraver
49 {
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_;
55
56   void set_melisma (bool);
57
58 protected:
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);
68
69   void acknowledge_extra_object (Grob_info);
70   void stop_translation_timestep ();
71   void process_music ();
72
73   virtual void finalize ();
74
75 public:
76   TRANSLATOR_DECLARATIONS (Slur_engraver);
77 };
78
79 Slur_engraver::Slur_engraver ()
80 {
81 }
82
83 IMPLEMENT_TRANSLATOR_LISTENER (Slur_engraver, slur);
84 void
85 Slur_engraver::listen_slur (Stream_event *ev)
86 {
87   Direction d = to_dir (ev->get_property ("span-direction"));
88   if (d == START)
89     start_events_.push_back (ev);
90   else if (d == STOP)
91     stop_events_.push_back (ev);
92   else ev->origin ()->warning (_f ("direction of %s invalid: %d",
93                                      "slur-event", int (d)));
94 }
95
96 void
97 Slur_engraver::set_melisma (bool m)
98 {
99   context ()->set_property ("slurMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
100 }
101
102 void
103 Slur_engraver::acknowledge_note_column (Grob_info info)
104 {
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);
110 }
111
112 void
113 Slur_engraver::acknowledge_extra_object (Grob_info info)
114 {
115   objects_to_acknowledge_.push_back (info);
116 }
117
118 void
119 Slur_engraver::acknowledge_inline_accidental (Grob_info info)
120 {
121   acknowledge_extra_object (info);
122 }
123
124 void
125 Slur_engraver::acknowledge_dots (Grob_info info)
126 {
127   acknowledge_extra_object (info);
128 }
129
130 void
131 Slur_engraver::acknowledge_fingering (Grob_info info)
132 {
133   acknowledge_extra_object (info);
134 }
135
136 void
137 Slur_engraver::acknowledge_tuplet_number (Grob_info info)
138 {
139   acknowledge_extra_object (info);
140 }
141
142 void
143 Slur_engraver::acknowledge_script (Grob_info info)
144 {
145   if (!info.grob ()->internal_has_interface (ly_symbol2scm ("dynamic-interface")))
146     acknowledge_extra_object (info);
147 }
148
149 void
150 Slur_engraver::acknowledge_text_script (Grob_info info)
151 {
152   acknowledge_extra_object (info);
153 }
154
155 void
156 Slur_engraver::acknowledge_tie (Grob_info info)
157 {
158   acknowledge_extra_object (info);
159 }
160
161 void
162 Slur_engraver::finalize ()
163 {
164   for (vsize i = 0; i < slurs_.size (); i++)
165     {
166       slurs_[i]->warning (_ ("unterminated slur"));
167       slurs_[i]->suicide ();
168     }
169 }
170
171 void
172 Slur_engraver::process_music ()
173 {
174   for (vsize i = 0; i < stop_events_.size (); i++)
175     {
176       Stream_event *ev = stop_events_[i];
177       string id = robust_scm2string (ev->get_property ("spanner-id"), "");
178
179       // Find the slurs that are ended with this event (by checking the spanner-id)
180       bool ended = false;
181       for (vsize j = slurs_.size (); j--;)
182         {
183           if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
184             {
185               ended = true;
186               end_slurs_.push_back (slurs_[j]);
187               slurs_.erase (slurs_.begin () + j);
188             }
189         }
190       if (ended)
191         {
192           // Ignore redundant stop events for this id
193           for (vsize j = stop_events_.size (); --j > i;)
194             {
195               if (id == robust_scm2string (stop_events_[j]->get_property ("spanner-id"), ""))
196                 stop_events_.erase (stop_events_.begin() + j);
197             }
198         }
199       else
200         ev->origin ()->warning (_ ("cannot end slur"));
201     }
202
203   vsize old_slurs = slurs_.size ();
204   for (vsize i = start_events_.size (); i--;)
205     {
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"));
209
210       bool completed;
211       for (vsize j = 0; !(completed = (j == slurs_.size ())); j++)
212         {
213           // Check if we already have a slur with the same spanner-id.
214           if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
215             {
216               if (j < old_slurs)
217                 {
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);
222                   break;
223                 }
224
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.
239
240               if (!updown)
241                 break;
242
243               // If the existing slur does not have a direction yet,
244               // give it ours
245
246               Direction slur_dir = to_dir (slurs_[j]->get_property ("direction"));
247
248               if (!slur_dir)
249                 {
250                   set_grob_direction (slurs_[j], updown);
251                   break;
252                 }
253
254               // If the existing slur has the same direction as ours, drop ours
255
256               if (slur_dir == updown)
257                 break;
258             }
259         }
260       // If the loop completed, our slur is new
261       if (completed)
262         {
263           Grob *slur = make_spanner ("Slur", ev->self_scm ());
264           slur->set_property ("spanner-id", ly_string2scm (id));
265           if (updown)
266             set_grob_direction (slur, updown);
267           slurs_.push_back (slur);
268
269           if (to_boolean (get_property ("doubleSlurs")))
270             {
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);
276             }
277         }
278     }
279   set_melisma (slurs_.size ());
280 }
281
282 void
283 Slur_engraver::stop_translation_timestep ()
284 {
285   if (Grob *g = unsmob_grob (get_property ("currentCommandColumn")))
286     {
287       for (vsize i = 0; i < end_slurs_.size (); i++)
288         Slur::add_extra_encompass (end_slurs_[i], g);
289
290       if (!start_events_.size ())
291         for (vsize i = 0; i < slurs_.size (); i++)
292           Slur::add_extra_encompass (slurs_[i], g);
293     }
294
295   for (vsize i = 0; i < end_slurs_.size (); i++)
296     {
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);
301     }
302
303   for (vsize i = 0; i < objects_to_acknowledge_.size (); i++)
304     Slur::auxiliary_acknowledge_extra_object (objects_to_acknowledge_[i], slurs_, end_slurs_);
305
306   objects_to_acknowledge_.clear ();
307   end_slurs_.clear ();
308   start_events_.clear ();
309   stop_events_.clear ();
310 }
311
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,
321                 /* doc */
322                 "Build slur grobs from slur events.",
323
324                 /* create */
325                 "Slur ",
326
327                 /* read */
328                 "slurMelismaBusy "
329                 "doubleSlurs ",
330
331                 /* write */
332                 ""
333                );