2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "translator.hh"
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "translator-group.hh"
29 #include "translator.icc"
34 Translator::~Translator ()
46 Translator::process_music ()
51 Translator::process_acknowledged ()
55 Translator::Translator ()
60 Translator::Translator (Translator const &)
67 Translator::now_mom () const
69 return daddy_context_->now_mom ();
73 Translator::get_output_def () const
75 return daddy_context_->get_output_def ();
79 Translator::get_daddy_translator () const
81 return daddy_context_->implementation ();
85 Translator::protect_event (SCM ev)
87 get_daddy_translator ()->protect_event (ev);
91 Translator::internal_get_property (SCM sym) const
93 return daddy_context_->internal_get_property (sym);
97 Translator::stop_translation_timestep ()
102 this function is called once each moment, before any user
103 information enters the translators. (i.e. no \property or event has
107 Translator::start_translation_timestep ()
112 Translator::initialize ()
117 Translator::finalize ()
122 Translator::connect_to_context (Context *c)
124 for (SCM r = get_listener_list (); scm_is_pair (r); r = scm_cdr (r))
126 SCM event_class = scm_caar (r);
127 SCM callback = scm_cdar (r);
129 c->events_below ()->add_listener (get_listener (callback),
135 Translator::disconnect_from_context (Context *c)
137 for (SCM r = get_listener_list (); scm_is_pair (r); r = scm_cdr (r))
139 SCM event_class = scm_caar (r);
140 SCM callback = scm_cdar (r);
142 c->events_below ()->remove_listener (get_listener (callback),
148 Translator::event_class_symbol (const char *ev_class)
150 /* ev_class is the C++ identifier name. Convert to scm symbol */
151 string name = string (ev_class);
152 name = replace_all (&name, '_', '-');
155 return scm_from_ascii_symbol (name.c_str ());
159 Helps the individual static_translator_description methods of translators.
162 Translator::static_translator_description (const char *grobs,
166 const char *write) const
168 SCM static_properties = SCM_EOL;
170 static_properties = scm_acons (ly_symbol2scm ("grobs-created"),
171 parse_symbol_list (grobs), static_properties);
173 static_properties = scm_acons (ly_symbol2scm ("description"),
174 scm_from_utf8_string (desc), static_properties);
177 for (; scm_is_pair (listener_list); listener_list = scm_cdr (listener_list))
178 list = scm_cons (scm_caar (listener_list), list);
179 static_properties = scm_acons (ly_symbol2scm ("events-accepted"),
180 list, static_properties);
182 static_properties = scm_acons (ly_symbol2scm ("properties-read"),
183 parse_symbol_list (read), static_properties);
185 static_properties = scm_acons (ly_symbol2scm ("properties-written"),
186 parse_symbol_list (write), static_properties);
188 return static_properties;
195 Translator::mark_smob () const
202 Translator::get_global_context () const
204 return daddy_context_->get_global_context ();
208 Translator::get_score_context () const
210 return daddy_context_->get_score_context ();
213 const char Translator::type_p_name_[] = "ly:translator?";
216 Translator::must_be_last () const
222 Translator::derived_mark () const
227 Translator::print_smob (SCM port, scm_print_state *) const
229 scm_puts ("#<Translator ", port);
230 scm_puts (class_name (), port);
231 scm_puts (" >", port);
236 add_acknowledger (Translator::Grob_info_callback ptr,
237 char const *func_name,
238 vector<Acknowledge_information> *ack_array)
240 Acknowledge_information inf;
243 string interface_name (func_name);
245 interface_name = replace_all (&interface_name, '_', '-');
246 interface_name += "-interface";
249 this is only called during program init, so safe to use scm_gc_protect_object ()
251 inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
252 ack_array->push_back (inf);
255 Translator::Grob_info_callback
256 generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
258 for (vsize i = 0; i < ack_array->size (); i++)
260 if (ack_array->at (i).symbol_ == sym)
261 return ack_array->at (i).function_;
267 get_event_length (Stream_event *e)
269 Moment *m = unsmob<Moment> (e->get_property ("length"));
277 get_event_length (Stream_event *e, Moment now)
279 Moment len = get_event_length (e);
283 len.grace_part_ = len.main_part_;
284 len.main_part_ = Rational (0);
290 Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
291 simultaneous events. The helper is only useful in listen_* methods
295 internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
298 && !to_boolean (scm_equal_p ((*old_ev)->self_scm (),
299 new_ev->self_scm ())))
301 /* extract event class from function name */
302 string ev_class = function;
304 /* This assertion fails if EVENT_ASSIGNMENT was called outside a
305 translator listener. Don't do that. */
306 const char *prefix = "listen_";
307 assert (0 == ev_class.find (prefix));
309 /* "listen_foo_bar" -> "foo-bar" */
310 ev_class.erase (0, strlen (prefix));
311 replace_all (&ev_class, '_', '-');
313 new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class.c_str ()));
314 (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class.c_str ()));
324 ADD_TRANSLATOR (Translator,
326 "Base class. Not instantiated.",