]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator.cc
Doc: Extending - 1.3.4 - clarify description for 'addAccent' function example
[lilypond.git] / lily / translator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "translator.hh"
21
22 #include "context-def.hh"
23 #include "dispatcher.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "translator-group.hh"
27 #include "warn.hh"
28
29 #include "translator.icc"
30
31 Translator::~Translator ()
32 {
33 }
34
35 void
36 Translator::init ()
37 {
38   daddy_context_ = 0;
39   smobify_self ();
40 }
41
42 void
43 Translator::process_music ()
44 {
45 }
46
47 void
48 Translator::process_acknowledged ()
49 {
50 }
51
52 Translator::Translator ()
53 {
54   init ();
55 }
56
57 Translator::Translator (Translator const &)
58   : Smob<Translator> ()
59 {
60   init ();
61 }
62
63 Moment
64 Translator::now_mom () const
65 {
66   return daddy_context_->now_mom ();
67 }
68
69 Output_def *
70 Translator::get_output_def () const
71 {
72   return daddy_context_->get_output_def ();
73 }
74
75 Translator_group *
76 Translator::get_daddy_translator () const
77 {
78   return daddy_context_->implementation ();
79 }
80
81 void
82 Translator::protect_event (SCM ev)
83 {
84   get_daddy_translator ()->protect_event (ev);
85 }
86
87 SCM
88 Translator::internal_get_property (SCM sym) const
89 {
90   return daddy_context_->internal_get_property (sym);
91 }
92
93 void
94 Translator::stop_translation_timestep ()
95 {
96 }
97
98 /*
99   this function is called once each moment, before any user
100   information enters the translators.  (i.e. no \property or event has
101   been processed yet.)
102 */
103 void
104 Translator::start_translation_timestep ()
105 {
106 }
107
108 void
109 Translator::initialize ()
110 {
111 }
112
113 void
114 Translator::finalize ()
115 {
116 }
117
118 void
119 Translator::connect_to_context (Context *c)
120 {
121   for (SCM r = get_listener_list (); scm_is_pair (r); r = scm_cdr (r))
122     {
123       SCM event_class = scm_caar (r);
124       SCM callback = scm_cdar (r);
125
126       c->events_below ()->add_listener (get_listener (callback),
127                                         event_class);
128     }
129 }
130
131 void
132 Translator::disconnect_from_context (Context *c)
133 {
134   for (SCM r = get_listener_list (); scm_is_pair (r); r = scm_cdr (r))
135     {
136       SCM event_class = scm_caar (r);
137       SCM callback = scm_cdar (r);
138
139       c->events_below ()->remove_listener (get_listener (callback),
140                                            event_class);
141     }
142 }
143
144 SCM
145 Translator::event_class_symbol (const char *ev_class)
146 {
147   /* ev_class is the C++ identifier name. Convert to scm symbol */
148   string name = string (ev_class);
149   name = replace_all (&name, '_', '-');
150   name += "-event";
151
152   return scm_from_ascii_symbol (name.c_str ());
153 }
154
155 /*
156  Helps the individual static_translator_description methods of translators.
157 */
158 SCM
159 Translator::static_translator_description (const char *grobs,
160                                            const char *desc,
161                                            SCM listener_list,
162                                            const char *read,
163                                            const char *write) const
164 {
165   SCM static_properties = SCM_EOL;
166
167   static_properties = scm_acons (ly_symbol2scm ("grobs-created"),
168                                  parse_symbol_list (grobs), static_properties);
169
170   static_properties = scm_acons (ly_symbol2scm ("description"),
171                                  scm_from_utf8_string (desc), static_properties);
172
173   SCM list = SCM_EOL;
174   for (; scm_is_pair (listener_list); listener_list = scm_cdr (listener_list))
175     list = scm_cons (scm_caar (listener_list), list);
176   static_properties = scm_acons (ly_symbol2scm ("events-accepted"),
177                                  list, static_properties);
178
179   static_properties = scm_acons (ly_symbol2scm ("properties-read"),
180                                  parse_symbol_list (read), static_properties);
181
182   static_properties = scm_acons (ly_symbol2scm ("properties-written"),
183                                  parse_symbol_list (write), static_properties);
184
185   return static_properties;
186 }
187
188 /*
189   SMOBS
190 */
191 SCM
192 Translator::mark_smob () const
193 {
194   derived_mark ();
195   return SCM_EOL;
196 }
197
198 Global_context *
199 Translator::get_global_context () const
200 {
201   return daddy_context_->get_global_context ();
202 }
203
204 Context *
205 Translator::get_score_context () const
206 {
207   return daddy_context_->get_score_context ();
208 }
209
210 const char Translator::type_p_name_[] = "ly:translator?";
211
212 bool
213 Translator::must_be_last () const
214 {
215   return false;
216 }
217
218 void
219 Translator::derived_mark () const
220 {
221 }
222
223 int
224 Translator::print_smob (SCM port, scm_print_state *) const
225 {
226   scm_puts ("#<Translator ", port);
227   scm_puts (class_name (), port);
228   scm_puts (" >", port);
229   return 1;
230 }
231
232 void
233 add_acknowledger (Translator::Grob_info_callback ptr,
234                   char const *func_name,
235                   vector<Acknowledge_information> *ack_array)
236 {
237   Acknowledge_information inf;
238   inf.function_ = ptr;
239
240   string interface_name (func_name);
241
242   interface_name = replace_all (&interface_name, '_', '-');
243   interface_name += "-interface";
244
245   /*
246     this is only called during program init, so safe to use scm_gc_protect_object ()
247   */
248   inf.symbol_ = scm_gc_protect_object (ly_symbol2scm (interface_name.c_str ()));
249   ack_array->push_back (inf);
250 }
251
252 Translator::Grob_info_callback
253 generic_get_acknowledger (SCM sym, vector<Acknowledge_information> const *ack_array)
254 {
255   for (vsize i = 0; i < ack_array->size (); i++)
256     {
257       if (ack_array->at (i).symbol_ == sym)
258         return ack_array->at (i).function_;
259     }
260   return 0;
261 }
262
263 Moment
264 get_event_length (Stream_event *e)
265 {
266   Moment *m = unsmob<Moment> (e->get_property ("length"));
267   if (m)
268     return *m;
269   else
270     return Moment (0);
271 }
272
273 Moment
274 get_event_length (Stream_event *e, Moment now)
275 {
276   Moment len = get_event_length (e);
277
278   if (now.grace_part_)
279     {
280       len.grace_part_ = len.main_part_;
281       len.main_part_ = Rational (0);
282     }
283   return len;
284 }
285
286 /*
287   Helper, used through ASSIGN_EVENT_ONCE to throw warnings for
288   simultaneous events. The helper is only useful in listen_* methods
289   of translators.
290 */
291 bool
292 internal_event_assignment (Stream_event **old_ev, Stream_event *new_ev, const char *function)
293 {
294   if (*old_ev
295       && !to_boolean (scm_equal_p ((*old_ev)->self_scm (),
296                                    new_ev->self_scm ())))
297     {
298       /* extract event class from function name */
299       string ev_class = function;
300
301       /* This assertion fails if EVENT_ASSIGNMENT was called outside a
302          translator listener. Don't do that. */
303       const char *prefix = "listen_";
304       assert (0 == ev_class.find (prefix));
305
306       /* "listen_foo_bar" -> "foo-bar" */
307       ev_class.erase (0, strlen (prefix));
308       replace_all (&ev_class, '_', '-');
309
310       new_ev->origin ()->warning (_f ("Two simultaneous %s events, junking this one", ev_class.c_str ()));
311       (*old_ev)->origin ()->warning (_f ("Previous %s event here", ev_class.c_str ()));
312       return false;
313     }
314   else
315     {
316       *old_ev = new_ev;
317       return true;
318     }
319 }
320
321 ADD_TRANSLATOR (Translator,
322                 /* doc */
323                 "Base class.  Not instantiated.",
324
325                 /* create */
326                 "",
327
328                 /* read */
329                 "",
330
331                 /* write */
332                 ""
333                );