]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-engraver.cc
Web-it: replace "open source" with "software libero"
[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   virtual void derived_mark () const;
75
76 public:
77   TRANSLATOR_DECLARATIONS (Slur_engraver);
78 };
79
80 Slur_engraver::Slur_engraver ()
81 {
82 }
83
84 void
85 Slur_engraver::derived_mark () const
86 {
87   for (vsize i=start_events_.size(); i--;)
88     scm_gc_mark (start_events_[i]->self_scm ());
89   for (vsize i=stop_events_.size(); i--;)
90     scm_gc_mark (stop_events_[i]->self_scm ());
91 }
92
93 IMPLEMENT_TRANSLATOR_LISTENER (Slur_engraver, slur);
94 void
95 Slur_engraver::listen_slur (Stream_event *ev)
96 {
97   Direction d = to_dir (ev->get_property ("span-direction"));
98   if (d == START)
99     start_events_.push_back (ev);
100   else if (d == STOP)
101     stop_events_.push_back (ev);
102   else ev->origin ()->warning (_f ("direction of %s invalid: %d",
103                                      "slur-event", int (d)));
104 }
105
106 void
107 Slur_engraver::set_melisma (bool m)
108 {
109   context ()->set_property ("slurMelismaBusy", m ? SCM_BOOL_T : SCM_BOOL_F);
110 }
111
112 void
113 Slur_engraver::acknowledge_note_column (Grob_info info)
114 {
115   Grob *e = info.grob ();
116   for (vsize i = slurs_.size (); i--;)
117     Slur::add_column (slurs_[i], e);
118   for (vsize i = end_slurs_.size (); i--;)
119     Slur::add_column (end_slurs_[i], e);
120 }
121
122 void
123 Slur_engraver::acknowledge_extra_object (Grob_info info)
124 {
125   objects_to_acknowledge_.push_back (info);
126 }
127
128 void
129 Slur_engraver::acknowledge_inline_accidental (Grob_info info)
130 {
131   acknowledge_extra_object (info);
132 }
133
134 void
135 Slur_engraver::acknowledge_dots (Grob_info info)
136 {
137   acknowledge_extra_object (info);
138 }
139
140 void
141 Slur_engraver::acknowledge_fingering (Grob_info info)
142 {
143   acknowledge_extra_object (info);
144 }
145
146 void
147 Slur_engraver::acknowledge_tuplet_number (Grob_info info)
148 {
149   acknowledge_extra_object (info);
150 }
151
152 void
153 Slur_engraver::acknowledge_script (Grob_info info)
154 {
155   if (!info.grob ()->internal_has_interface (ly_symbol2scm ("dynamic-interface")))
156     acknowledge_extra_object (info);
157 }
158
159 void
160 Slur_engraver::acknowledge_text_script (Grob_info info)
161 {
162   acknowledge_extra_object (info);
163 }
164
165 void
166 Slur_engraver::acknowledge_tie (Grob_info info)
167 {
168   acknowledge_extra_object (info);
169 }
170
171 void
172 Slur_engraver::finalize ()
173 {
174   for (vsize i = 0; i < slurs_.size (); i++)
175     {
176       slurs_[i]->warning (_ ("unterminated slur"));
177       slurs_[i]->suicide ();
178     }
179   slurs_.clear ();
180 }
181
182 void
183 Slur_engraver::process_music ()
184 {
185   for (vsize i = 0; i < stop_events_.size (); i++)
186     {
187       Stream_event *ev = stop_events_[i];
188       string id = robust_scm2string (ev->get_property ("spanner-id"), "");
189
190       // Find the slurs that are ended with this event (by checking the spanner-id)
191       bool ended = false;
192       for (vsize j = slurs_.size (); j--;)
193         {
194           if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
195             {
196               ended = true;
197               end_slurs_.push_back (slurs_[j]);
198               slurs_.erase (slurs_.begin () + j);
199             }
200         }
201       if (ended)
202         {
203           // Ignore redundant stop events for this id
204           for (vsize j = stop_events_.size (); --j > i;)
205             {
206               if (id == robust_scm2string (stop_events_[j]->get_property ("spanner-id"), ""))
207                 stop_events_.erase (stop_events_.begin() + j);
208             }
209         }
210       else
211         ev->origin ()->warning (_ ("cannot end slur"));
212     }
213
214   vsize old_slurs = slurs_.size ();
215   for (vsize i = start_events_.size (); i--;)
216     {
217       Stream_event *ev = start_events_[i];
218       string id = robust_scm2string (ev->get_property ("spanner-id"), "");
219       Direction updown = to_dir (ev->get_property ("direction"));
220
221       bool completed;
222       for (vsize j = 0; !(completed = (j == slurs_.size ())); j++)
223         {
224           // Check if we already have a slur with the same spanner-id.
225           if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
226             {
227               if (j < old_slurs)
228                 {
229                   // We already have an old slur, so give a warning
230                   // and completely ignore the new slur.
231                   ev->origin ()->warning (_ ("already have slur"));
232                   start_events_.erase (start_events_.begin () + i);
233                   break;
234                 }
235
236               // If this slur event has no direction, it will not
237               // contribute anything new to the existing slur(s), so
238               // we can ignore it.  This is not entirely accurate:
239               // tweaks or context properties like those set with
240               // \slurUp can still override a neutral direction, so
241               // when encountering a slur event with "opposite"
242               // direction first, then one with neutral direction, we
243               // only let the "opposite" direction remain, while if
244               // the order is the other way round, a double slur
245               // results since the direction of the first slur is no
246               // longer attributable to a "neutral" slur event.  A
247               // mixture of neutral and directed events is nothing
248               // that the partcombiner should crank out, and it would
249               // be decidedly strange for manual input.
250
251               if (!updown)
252                 break;
253
254               // If the existing slur does not have a direction yet,
255               // give it ours
256
257               Direction slur_dir = to_dir (slurs_[j]->get_property ("direction"));
258
259               if (!slur_dir)
260                 {
261                   set_grob_direction (slurs_[j], updown);
262                   break;
263                 }
264
265               // If the existing slur has the same direction as ours, drop ours
266
267               if (slur_dir == updown)
268                 break;
269             }
270         }
271       // If the loop completed, our slur is new
272       if (completed)
273         {
274           Grob *slur = make_spanner ("Slur", ev->self_scm ());
275           slur->set_property ("spanner-id", ly_string2scm (id));
276           if (updown)
277             set_grob_direction (slur, updown);
278           slurs_.push_back (slur);
279
280           if (to_boolean (get_property ("doubleSlurs")))
281             {
282               set_grob_direction (slur, DOWN);
283               slur = make_spanner ("Slur", ev->self_scm ());
284               slur->set_property ("spanner-id", ly_string2scm (id));
285               set_grob_direction (slur, UP);
286               slurs_.push_back (slur);
287             }
288         }
289     }
290   set_melisma (slurs_.size ());
291 }
292
293 void
294 Slur_engraver::stop_translation_timestep ()
295 {
296   if (Grob *g = unsmob_grob (get_property ("currentCommandColumn")))
297     {
298       for (vsize i = 0; i < end_slurs_.size (); i++)
299         Slur::add_extra_encompass (end_slurs_[i], g);
300
301       if (!start_events_.size ())
302         for (vsize i = 0; i < slurs_.size (); i++)
303           Slur::add_extra_encompass (slurs_[i], g);
304     }
305
306   for (vsize i = 0; i < end_slurs_.size (); i++)
307     {
308       Spanner *s = dynamic_cast<Spanner *> (end_slurs_[i]);
309       if (!s->get_bound (RIGHT))
310         s->set_bound (RIGHT, unsmob_grob (get_property ("currentMusicalColumn")));
311       announce_end_grob (s, SCM_EOL);
312     }
313
314   for (vsize i = 0; i < objects_to_acknowledge_.size (); i++)
315     Slur::auxiliary_acknowledge_extra_object (objects_to_acknowledge_[i], slurs_, end_slurs_);
316
317   objects_to_acknowledge_.clear ();
318   end_slurs_.clear ();
319   start_events_.clear ();
320   stop_events_.clear ();
321 }
322
323 ADD_ACKNOWLEDGER (Slur_engraver, inline_accidental);
324 ADD_ACKNOWLEDGER (Slur_engraver, fingering);
325 ADD_ACKNOWLEDGER (Slur_engraver, note_column);
326 ADD_ACKNOWLEDGER (Slur_engraver, script);
327 ADD_ACKNOWLEDGER (Slur_engraver, text_script);
328 ADD_ACKNOWLEDGER (Slur_engraver, dots);
329 ADD_ACKNOWLEDGER (Slur_engraver, tie);
330 ADD_ACKNOWLEDGER (Slur_engraver, tuplet_number);
331 ADD_TRANSLATOR (Slur_engraver,
332                 /* doc */
333                 "Build slur grobs from slur events.",
334
335                 /* create */
336                 "Slur ",
337
338                 /* read */
339                 "slurMelismaBusy "
340                 "doubleSlurs ",
341
342                 /* write */
343                 ""
344                );