]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-dynamic-engraver.cc
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / new-dynamic-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2008--2009 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 New_dynamic_engraver : public Engraver
34 {
35   TRANSLATOR_DECLARATIONS (New_dynamic_engraver);
36   DECLARE_ACKNOWLEDGER (note_column);
37   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
38   DECLARE_TRANSLATOR_LISTENER (span_dynamic);
39
40 protected:
41   virtual void process_music ();
42   virtual void stop_translation_timestep ();
43   virtual void finalize ();
44
45 private:
46   SCM get_property_setting (Stream_event *evt, char const *evprop,
47                             char const *ctxprop);
48   string get_spanner_type (Stream_event *ev);
49
50   Drul_array<Stream_event *> accepted_spanevents_drul_;
51   Spanner *current_spanner_;
52   Spanner *finished_spanner_;
53
54   Item *script_;
55   Stream_event *script_event_;
56   Stream_event *current_span_event_;
57 };
58
59 New_dynamic_engraver::New_dynamic_engraver ()
60 {
61   script_event_ = 0;
62   current_span_event_ = 0;
63   script_ = 0;
64   finished_spanner_ = 0;
65   current_spanner_ = 0;
66   accepted_spanevents_drul_.set (0, 0);
67 }
68
69 IMPLEMENT_TRANSLATOR_LISTENER (New_dynamic_engraver, absolute_dynamic);
70 void
71 New_dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
72 {
73   ASSIGN_EVENT_ONCE (script_event_, ev);
74 }
75
76 IMPLEMENT_TRANSLATOR_LISTENER (New_dynamic_engraver, span_dynamic);
77 void
78 New_dynamic_engraver::listen_span_dynamic (Stream_event *ev)
79 {
80   Direction d = to_dir (ev->get_property ("span-direction"));
81
82   ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[d], ev);
83 }
84
85 SCM
86 New_dynamic_engraver::get_property_setting (Stream_event *evt,
87                                             char const *evprop,
88                                             char const *ctxprop)
89 {
90   SCM spanner_type = evt->get_property (evprop);
91   if (spanner_type == SCM_EOL)
92     spanner_type = get_property (ctxprop);
93   return spanner_type;
94 }
95
96 void
97 New_dynamic_engraver::process_music ()
98 {
99   if (current_spanner_
100       && (accepted_spanevents_drul_[STOP]
101           || script_event_
102           || accepted_spanevents_drul_[START]))
103     {
104       Stream_event *ender = accepted_spanevents_drul_[STOP];
105       if (!ender)
106         ender = script_event_;
107
108       if (!ender)
109         ender = accepted_spanevents_drul_[START];
110
111       finished_spanner_ = current_spanner_;
112       announce_end_grob (finished_spanner_, ender->self_scm ());
113       current_spanner_ = 0;
114       current_span_event_ = 0;
115     }
116
117   if (accepted_spanevents_drul_[START])
118     {
119       current_span_event_ = accepted_spanevents_drul_[START];
120
121       string start_type = get_spanner_type (current_span_event_);
122       SCM cresc_type = get_property_setting (current_span_event_, "span-type",
123                                              (start_type + "Spanner").c_str ());
124
125       if (cresc_type == ly_symbol2scm ("text"))
126         {
127           current_spanner_
128             = make_spanner ("DynamicTextSpanner",
129                             accepted_spanevents_drul_[START]->self_scm ());
130
131           SCM text = get_property_setting (current_span_event_, "span-text",
132                                            (start_type + "Text").c_str ());
133           if (Text_interface::is_markup (text))
134             current_spanner_->set_property ("text", text);
135         }
136       else
137         {
138           if (cresc_type != ly_symbol2scm ("hairpin"))
139             {
140               string as_string = ly_scm_write_string (cresc_type);
141               current_span_event_
142                 ->origin()->warning (_f ("unknown crescendo style: %s\ndefaulting to hairpin.", as_string.c_str()));
143             }
144           current_spanner_ = make_spanner ("Hairpin",
145                                            current_span_event_->self_scm ());
146         }
147       if (finished_spanner_)
148         {
149           if (Hairpin::has_interface (finished_spanner_))
150             Pointer_group_interface::add_grob (finished_spanner_,
151                                                ly_symbol2scm ("adjacent-spanners"),
152                                                current_spanner_);
153           if (Hairpin::has_interface (current_spanner_))
154             Pointer_group_interface::add_grob (current_spanner_,
155                                                ly_symbol2scm ("adjacent-spanners"),
156                                                finished_spanner_);
157         }
158     }
159
160   if (script_event_)
161     {
162       script_ = make_item ("DynamicText", script_event_->self_scm ());
163       script_->set_property ("text",
164                              script_event_->get_property ("text"));
165
166       if (finished_spanner_)
167         finished_spanner_->set_bound (RIGHT, script_);
168       if (current_spanner_)
169         {
170           current_spanner_->set_bound (LEFT, script_);
171
172           if (!Hairpin::has_interface (current_spanner_))
173             set_nested_property (current_spanner_,
174                                  scm_list_3 (ly_symbol2scm ("bound-details"),
175                                              ly_symbol2scm ("left"),
176                                              ly_symbol2scm ("attach-dir")),
177                                  scm_from_int (RIGHT));
178         }
179     }
180 }
181
182 void
183 New_dynamic_engraver::stop_translation_timestep ()
184 {
185   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
186     finished_spanner_
187       ->set_bound (RIGHT,
188                    unsmob_grob (get_property ("currentMusicalColumn")));
189
190   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
191     current_spanner_
192       ->set_bound (LEFT,
193                    unsmob_grob (get_property ("currentMusicalColumn")));
194   script_ = 0;
195   script_event_ = 0;
196   accepted_spanevents_drul_.set (0, 0);
197   finished_spanner_ = 0;
198 }
199
200 void
201 New_dynamic_engraver::finalize ()
202 {
203   if (current_spanner_
204       && !current_spanner_->is_live ())
205     current_spanner_ = 0;
206   if (current_spanner_)
207     {
208       current_span_event_
209         ->origin ()->warning (_f ("unterminated %s",
210                                   get_spanner_type (current_span_event_)
211                                   .c_str ()));
212       current_spanner_->suicide ();
213       current_spanner_ = 0;
214     }
215 }
216
217 string
218 New_dynamic_engraver::get_spanner_type (Stream_event *ev)
219 {
220   string type;
221   SCM start_sym = ev->get_property ("class");
222
223   if (start_sym == ly_symbol2scm ("decrescendo-event"))
224     type = "decrescendo";
225   else if (start_sym == ly_symbol2scm ("crescendo-event"))
226     type = "crescendo";
227   else
228     programming_error ("unknown dynamic spanner type");
229
230   return type;
231 }
232
233 void
234 New_dynamic_engraver::acknowledge_note_column (Grob_info info)
235 {
236   if (script_ && !script_->get_parent (X_AXIS))
237     {
238       extract_grob_set (info.grob (), "note-heads", heads);
239       if (heads.size ())
240         {
241           Grob *head = heads[0];
242           script_->set_parent (head, X_AXIS);
243           Self_alignment_interface::set_center_parent (script_, X_AXIS);
244         }
245     }
246
247   if (current_spanner_ && !current_spanner_->get_bound (LEFT))
248     current_spanner_->set_bound (LEFT, info.grob ());
249   if (finished_spanner_ && !finished_spanner_->get_bound (RIGHT))
250     finished_spanner_->set_bound (RIGHT, info.grob ());
251 }
252
253 ADD_ACKNOWLEDGER (New_dynamic_engraver, note_column);
254 ADD_TRANSLATOR (New_dynamic_engraver,
255                 /* doc */
256                 "Create hairpins, dynamic texts, and their vertical"
257                 " alignments.  The symbols are collected onto a"
258                 " @code{DynamicLineSpanner} grob which takes care of vertical"
259                 " positioning.",
260
261                 /* create */
262                 "DynamicTextSpanner "
263                 "DynamicText "
264                 "Hairpin ",
265
266                 /* read */
267                 "crescendoSpanner "
268                 "crescendoText "
269                 "currentMusicalColumn "
270                 "decrescendoSpanner "
271                 "decrescendoText ",
272
273                 /* write */
274                 ""
275                 );