]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
Issue 4814: grob.cc segfaults with gcc6
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2015 Han-Wen Nienhuys <hanwen@lilypond.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 #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"
30
31 #include "translator.icc"
32
33 class Dynamic_engraver : public Engraver
34 {
35   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
36   void acknowledge_note_column (Grob_info);
37   void listen_absolute_dynamic (Stream_event *);
38   void listen_span_dynamic (Stream_event *);
39   void listen_break_span (Stream_event *);
40
41 protected:
42   virtual void process_music ();
43   virtual void stop_translation_timestep ();
44   virtual void finalize ();
45
46 private:
47   SCM get_property_setting (Stream_event *evt, char const *evprop,
48                             char const *ctxprop);
49   string get_spanner_type (Stream_event *ev);
50
51   Drul_array<Stream_event *> accepted_spanevents_drul_;
52   Spanner *current_spanner_;
53   Spanner *finished_spanner_;
54
55   Item *script_;
56   Stream_event *script_event_;
57   Stream_event *current_span_event_;
58   bool end_new_spanner_;
59 };
60
61 Dynamic_engraver::Dynamic_engraver ()
62 {
63   script_event_ = 0;
64   current_span_event_ = 0;
65   script_ = 0;
66   finished_spanner_ = 0;
67   current_spanner_ = 0;
68   accepted_spanevents_drul_.set (0, 0);
69   end_new_spanner_ = false;
70 }
71
72 void
73 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
74 {
75   ASSIGN_EVENT_ONCE (script_event_, ev);
76 }
77
78 void
79 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
80 {
81   Direction d = to_dir (ev->get_property ("span-direction"));
82
83   ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
84 }
85
86 void
87 Dynamic_engraver::listen_break_span (Stream_event *event)
88 {
89   if (event->in_event_class ("break-dynamic-span-event"))
90     {
91       // Case 1: Already have a start dynamic event -> break applies to new
92       //         spanner (created later) -> set a flag
93       // Case 2: no new spanner, but spanner already active -> break it now
94       if (accepted_spanevents_drul_[START])
95         end_new_spanner_ = true;
96       else if (current_spanner_)
97         current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
98     }
99 }
100
101 SCM
102 Dynamic_engraver::get_property_setting (Stream_event *evt,
103                                         char const *evprop,
104                                         char const *ctxprop)
105 {
106   SCM spanner_type = evt->get_property (evprop);
107   if (scm_is_null (spanner_type))
108     spanner_type = get_property (ctxprop);
109   return spanner_type;
110 }
111
112 void
113 Dynamic_engraver::process_music ()
114 {
115   if (current_spanner_
116       && (accepted_spanevents_drul_[STOP]
117           || script_event_
118           || accepted_spanevents_drul_[START]))
119     {
120       Stream_event *ender = accepted_spanevents_drul_[STOP];
121       if (!ender)
122         ender = script_event_;
123
124       if (!ender)
125         ender = accepted_spanevents_drul_[START];
126
127       finished_spanner_ = current_spanner_;
128       announce_end_grob (finished_spanner_, ender->self_scm ());
129       current_spanner_ = 0;
130       current_span_event_ = 0;
131     }
132
133   if (accepted_spanevents_drul_[START])
134     {
135       current_span_event_ = accepted_spanevents_drul_[START];
136
137       string start_type = get_spanner_type (current_span_event_);
138       SCM cresc_type = get_property_setting (current_span_event_, "span-type",
139                                              (start_type + "Spanner").c_str ());
140
141       if (scm_is_eq (cresc_type, ly_symbol2scm ("text")))
142         {
143           current_spanner_
144             = make_spanner ("DynamicTextSpanner",
145                             accepted_spanevents_drul_[START]->self_scm ());
146
147           SCM text = get_property_setting (current_span_event_, "span-text",
148                                            (start_type + "Text").c_str ());
149           if (Text_interface::is_markup (text))
150             current_spanner_->set_property ("text", text);
151           /*
152             If the line of a text spanner is hidden, end the alignment spanner
153             early: this allows dynamics to be spaced individually instead of
154             being linked together.
155           */
156           if (scm_is_eq (current_spanner_->get_property ("style"),
157                          ly_symbol2scm ("none")))
158             current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
159         }
160       else
161         {
162           if (!scm_is_eq (cresc_type, ly_symbol2scm ("hairpin")))
163             {
164               string as_string = ly_scm_write_string (cresc_type);
165               current_span_event_
166               ->origin ()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str ()));
167             }
168           current_spanner_ = make_spanner ("Hairpin",
169                                            current_span_event_->self_scm ());
170         }
171       // if we have a break-dynamic-span event right after the start dynamic, break the new spanner immediately
172       if (end_new_spanner_)
173         {
174           current_spanner_->set_property ("spanner-broken", SCM_BOOL_T);
175           end_new_spanner_ = false;
176         }
177       if (finished_spanner_)
178         {
179           if (has_interface<Hairpin> (finished_spanner_))
180             Pointer_group_interface::add_grob (finished_spanner_,
181                                                ly_symbol2scm ("adjacent-spanners"),
182                                                current_spanner_);
183           if (has_interface<Hairpin> (current_spanner_))
184             Pointer_group_interface::add_grob (current_spanner_,
185                                                ly_symbol2scm ("adjacent-spanners"),
186                                                finished_spanner_);
187         }
188     }
189
190   if (script_event_)
191     {
192       script_ = make_item ("DynamicText", script_event_->self_scm ());
193       script_->set_property ("text",
194                              script_event_->get_property ("text"));
195
196       if (finished_spanner_)
197         finished_spanner_->set_bound (RIGHT, script_);
198       if (current_spanner_)
199         current_spanner_->set_bound (LEFT, script_);
200     }
201 }
202
203 void
204 Dynamic_engraver::stop_translation_timestep ()
205 {
206   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
207     finished_spanner_
208     ->set_bound (RIGHT,
209                  unsmob<Grob> (get_property ("currentMusicalColumn")));
210
211   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
212     current_spanner_
213     ->set_bound (LEFT,
214                  unsmob<Grob> (get_property ("currentMusicalColumn")));
215   script_ = 0;
216   script_event_ = 0;
217   accepted_spanevents_drul_.set (0, 0);
218   finished_spanner_ = 0;
219   end_new_spanner_ = false;
220 }
221
222 void
223 Dynamic_engraver::finalize ()
224 {
225   if (current_spanner_
226       && !current_spanner_->is_live ())
227     current_spanner_ = 0;
228   if (current_spanner_)
229     {
230       current_span_event_
231       ->origin ()->warning (_f ("unterminated %s",
232                                 get_spanner_type (current_span_event_)
233                                 .c_str ()));
234       current_spanner_->suicide ();
235       current_spanner_ = 0;
236     }
237 }
238
239 string
240 Dynamic_engraver::get_spanner_type (Stream_event *ev)
241 {
242   string type;
243   SCM start_sym = scm_car (ev->get_property ("class"));
244
245   if (scm_is_eq (start_sym, ly_symbol2scm ("decrescendo-event")))
246     type = "decrescendo";
247   else if (scm_is_eq (start_sym, ly_symbol2scm ("crescendo-event")))
248     type = "crescendo";
249   else
250     programming_error ("unknown dynamic spanner type");
251
252   return type;
253 }
254
255 void
256 Dynamic_engraver::acknowledge_note_column (Grob_info info)
257 {
258   if (script_ && !script_->get_parent (X_AXIS))
259     {
260       extract_grob_set (info.grob (), "note-heads", heads);
261       /*
262         Spacing constraints may require dynamics to be attached to rests,
263         so check for a rest if this note column has no note heads.
264       */
265       Grob *x_parent = (heads.size ()
266                         ? info.grob ()
267                         : unsmob<Grob> (info.grob ()->get_object ("rest")));
268       if (x_parent)
269         script_->set_parent (x_parent, X_AXIS);
270     }
271
272   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
273     current_spanner_->set_bound (LEFT, info.grob ());
274   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
275     finished_spanner_->set_bound (RIGHT, info.grob ());
276 }
277
278 void
279 Dynamic_engraver::boot ()
280 {
281   ADD_LISTENER (Dynamic_engraver, absolute_dynamic);
282   ADD_LISTENER (Dynamic_engraver, span_dynamic);
283   ADD_LISTENER (Dynamic_engraver, break_span);
284   ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
285 }
286
287 ADD_TRANSLATOR (Dynamic_engraver,
288                 /* doc */
289                 "Create hairpins, dynamic texts and dynamic text spanners.",
290
291                 /* create */
292                 "DynamicTextSpanner "
293                 "DynamicText "
294                 "Hairpin ",
295
296                 /* read */
297                 "crescendoSpanner "
298                 "crescendoText "
299                 "currentMusicalColumn "
300                 "decrescendoSpanner "
301                 "decrescendoText ",
302
303                 /* write */
304                 ""
305                );