]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-proto-engraver.cc
Issue 4625/1: Rename Slur_proto_engraver::internal_listen_slur to listen_slur
[lilypond.git] / lily / slur-proto-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2013--2015 Mike Solomon <mike@mikesolomon.org>
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 "slur-proto-engraver.hh"
28 #include "spanner.hh"
29 #include "stream-event.hh"
30 #include "warn.hh"
31
32 #include "translator.icc"
33
34 using std::string;
35
36 void
37 Slur_proto_engraver::derived_mark () const
38 {
39   for (vsize i = start_events_.size (); i--;)
40     scm_gc_mark (start_events_[i]->self_scm ());
41   for (vsize i = stop_events_.size (); i--;)
42     scm_gc_mark (stop_events_[i]->self_scm ());
43 }
44
45 void
46 Slur_proto_engraver::listen_slur (Stream_event *ev)
47 {
48   Direction d = to_dir (ev->get_property ("span-direction"));
49   if (d == START)
50     start_events_.push_back (ev);
51   else if (d == STOP)
52     stop_events_.push_back (ev);
53   else ev->origin ()->warning (_f ("direction of %s invalid: %d",
54                                      event_name_, int (d)));
55 }
56
57 void
58 Slur_proto_engraver::acknowledge_note_column (Grob_info info)
59 {
60   Grob *e = info.grob ();
61   for (vsize i = slurs_.size (); i--;)
62     Slur::add_column (slurs_[i], e);
63   for (vsize i = end_slurs_.size (); i--;)
64     Slur::add_column (end_slurs_[i], e);
65 }
66
67 void
68 Slur_proto_engraver::acknowledge_extra_object (Grob_info info)
69 {
70   objects_to_acknowledge_.push_back (info);
71 }
72
73 void
74 Slur_proto_engraver::acknowledge_inline_accidental (Grob_info info)
75 {
76   acknowledge_extra_object (info);
77 }
78
79 void
80 Slur_proto_engraver::acknowledge_dots (Grob_info info)
81 {
82   acknowledge_extra_object (info);
83 }
84
85 void
86 Slur_proto_engraver::acknowledge_fingering (Grob_info info)
87 {
88   acknowledge_extra_object (info);
89 }
90
91 void
92 Slur_proto_engraver::acknowledge_tuplet_number (Grob_info info)
93 {
94   acknowledge_extra_object (info);
95 }
96
97 void
98 Slur_proto_engraver::acknowledge_script (Grob_info info)
99 {
100   if (!info.grob ()->internal_has_interface (ly_symbol2scm ("dynamic-interface")))
101     acknowledge_extra_object (info);
102 }
103
104 void
105 Slur_proto_engraver::acknowledge_text_script (Grob_info info)
106 {
107   acknowledge_extra_object (info);
108 }
109
110 void
111 Slur_proto_engraver::acknowledge_end_tie (Grob_info info)
112 {
113   acknowledge_extra_object (info);
114 }
115
116 void
117 Slur_proto_engraver::finalize ()
118 {
119   for (vsize i = 0; i < slurs_.size (); i++)
120     {
121       slurs_[i]->warning (_f ("unterminated %s", object_name_));
122       slurs_[i]->suicide ();
123     }
124   slurs_.clear ();
125 }
126
127 void
128 Slur_proto_engraver::create_slur (const string &spanner_id, Stream_event *ev_cause, Grob *g_cause, Direction dir, bool left_broken)
129 {
130   Grob *ccc = unsmob<Grob> (get_property ("currentCommandColumn"));
131   SCM cause = ev_cause ? ev_cause->self_scm () : g_cause->self_scm ();
132   Spanner *slur = make_spanner (grob_name_, cause);
133   slur->set_property ("spanner-id", ly_string2scm (spanner_id));
134   if (dir)
135     set_grob_direction (slur, dir);
136   if (left_broken)
137     slur->set_bound (LEFT, ccc);
138   slurs_.push_back (slur);
139   if (double_property_name_
140       && to_boolean (get_property (double_property_name_)))
141   {
142     set_grob_direction (slur, DOWN);
143     slur = make_spanner (grob_name_, cause);
144     slur->set_property ("spanner-id", ly_string2scm (spanner_id));
145     set_grob_direction (slur, UP);
146     if (left_broken)
147       slur->set_bound (LEFT, ccc);
148     slurs_.push_back (slur);
149   }
150
151 }
152
153 bool
154 Slur_proto_engraver::can_create_slur (const string &id, vsize old_slurs, vsize *event_idx, Stream_event *ev)
155 {
156   for (vsize j = slurs_.size (); j--;)
157     {
158       Grob *slur = slurs_[j];
159       Direction updown = to_dir (ev->get_property ("direction"));
160
161       // Check if we already have a slur with the same spanner-id.
162       if (id == robust_scm2string (slur->get_property ("spanner-id"), ""))
163         {
164           if (j < old_slurs)
165             {
166               // We already have an old slur, so give a warning
167               // and completely ignore the new slur.
168               ev->origin ()->warning (_f ("already have %s", object_name_));
169               if (event_idx)
170                 start_events_.erase (start_events_.begin () + (*event_idx));
171               return false;
172             }
173
174           // If this slur event has no direction, it will not
175           // contribute anything new to the existing slur(s), so
176           // we can ignore it.
177
178           if (!updown)
179             return false;
180
181           Stream_event *c = unsmob<Stream_event> (slur->get_property ("cause"));
182
183           if (!c)
184             {
185               slur->programming_error (_f ("%s without a cause", object_name_));
186               return true;
187             }
188
189           Direction slur_dir = to_dir (c->get_property ("direction"));
190
191           // If the existing slur does not have a direction yet,
192           // we'd rather take the new one.
193
194           if (!slur_dir)
195             {
196               slur->suicide ();
197               slurs_.erase (slurs_.begin () + j);
198               return true;
199             }
200
201           // If the existing slur has the same direction as ours, drop ours
202
203           if (slur_dir == updown)
204             return false;
205         }
206     }
207   return true;
208 }
209
210 bool
211 Slur_proto_engraver::try_to_end (Stream_event *ev)
212 {
213   string id = robust_scm2string (ev->get_property ("spanner-id"), "");
214
215   // Find the slurs that are ended with this event (by checking the spanner-id)
216   bool ended = false;
217   for (vsize j = slurs_.size (); j--;)
218     {
219       if (id == robust_scm2string (slurs_[j]->get_property ("spanner-id"), ""))
220         {
221           ended = true;
222           end_slurs_.push_back (slurs_[j]);
223           slurs_.erase (slurs_.begin () + j);
224         }
225     }
226   return ended;
227 }
228
229 void
230 Slur_proto_engraver::process_music ()
231 {
232   for (vsize i = 0; i < stop_events_.size (); i++)
233     {
234       string id = robust_scm2string (stop_events_[i]->get_property ("spanner-id"), "");
235       bool ended = try_to_end (stop_events_[i]);
236       if (ended)
237         {
238           // Ignore redundant stop events for this id
239           for (vsize j = stop_events_.size (); --j > i;)
240             {
241               if (id == robust_scm2string (stop_events_[j]->get_property ("spanner-id"), ""))
242                 stop_events_.erase (stop_events_.begin () + j);
243             }
244         }
245       else
246         stop_events_[i]->origin ()->warning (_f ("cannot end %s", object_name_));
247     }
248
249   vsize old_slurs = slurs_.size ();
250   for (vsize i = start_events_.size (); i--;)
251     {
252       Stream_event *ev = start_events_[i];
253       string id = robust_scm2string (ev->get_property ("spanner-id"), "");
254       Direction updown = to_dir (ev->get_property ("direction"));
255
256       if (can_create_slur (id, old_slurs, &i, ev))
257         create_slur (id, ev, 0, updown, false);
258     }
259
260   set_melisma (slurs_.size ());
261 }
262
263 void
264 Slur_proto_engraver::set_melisma (bool)
265 {
266 }
267
268 void
269 Slur_proto_engraver::stop_translation_timestep ()
270 {
271   if (Grob *g = unsmob<Grob> (get_property ("currentCommandColumn")))
272     {
273       for (vsize i = 0; i < end_slurs_.size (); i++)
274         Slur::add_extra_encompass (end_slurs_[i], g);
275
276       if (!start_events_.size ())
277         for (vsize i = 0; i < slurs_.size (); i++)
278           Slur::add_extra_encompass (slurs_[i], g);
279     }
280
281   for (vsize i = 0; i < end_slurs_.size (); i++)
282     {
283       Spanner *s = dynamic_cast<Spanner *> (end_slurs_[i]);
284       if (!s->get_bound (RIGHT))
285         s->set_bound (RIGHT, unsmob<Grob> (get_property ("currentMusicalColumn")));
286       announce_end_grob (s, SCM_EOL);
287     }
288
289   for (vsize i = 0; i < objects_to_acknowledge_.size (); i++)
290     Slur::auxiliary_acknowledge_extra_object (objects_to_acknowledge_[i], slurs_, end_slurs_);
291
292   objects_to_acknowledge_.clear ();
293   end_slurs_.clear ();
294   start_events_.clear ();
295   stop_events_.clear ();
296 }
297
298 // no ADD_ACKNOWLEDGER / ADD_TRANSLATOR macro calls
299 // since this class is abstract