]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
* lily/bar-line.cc (compound_barline): add bartype : (dotted).
[lilypond.git] / lily / translator-group.cc
1 /*
2   Translator_group.cc -- implement Translator_group
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "music-output-def.hh"
10 #include "translator-group.hh"
11 #include "translator.hh"
12 #include "warn.hh"
13 #include "moment.hh"
14 #include "scm-hash.hh"
15 #include "translator-def.hh"
16 #include "main.hh"
17 #include "music.hh"
18
19 Translator_group::Translator_group (Translator_group const&s)
20   : Translator (s)
21 {
22   iterator_count_ =0;
23   
24   Scheme_hash_table * tab =  new Scheme_hash_table (*s.properties_dict ());
25   properties_scm_ = tab->self_scm ();
26   scm_gc_unprotect_object (tab->self_scm ());
27 }
28
29 Scheme_hash_table*
30 Translator_group::properties_dict () const
31 {
32   return Scheme_hash_table::unsmob (properties_scm_);
33 }
34
35 Translator_group::~Translator_group ()
36 {
37   
38   //assert (is_removable ());
39 }
40
41
42 Translator_group::Translator_group ()
43 {
44   iterator_count_  = 0;
45   Scheme_hash_table *tab = new Scheme_hash_table ;
46   properties_scm_ = tab->self_scm ();
47
48   scm_gc_unprotect_object (tab->self_scm ());
49 }
50
51 void
52 Translator_group::check_removal ()
53 {
54   SCM next = SCM_EOL; 
55   for (SCM p = trans_group_list_; gh_pair_p (p); p = next)
56     {
57       next = ly_cdr (p);
58
59       Translator_group *trg =  dynamic_cast<Translator_group*> (unsmob_translator (ly_car (p)));
60
61       trg->check_removal ();
62       if (trg->is_removable ())
63         terminate_translator (trg);
64     }
65 }
66
67 SCM
68 Translator_group::add_translator (SCM list, Translator *t)
69 {
70   /*
71     Must append, since list ordering must be preserved.
72    */
73   list = gh_append2 (list, gh_cons (t->self_scm (), SCM_EOL));
74   t->daddy_trans_ = this;
75   t->output_def_ = output_def_;
76
77   return list;
78 }
79
80
81 void
82 Translator_group::add_used_group_translator (Translator *t)
83 {
84   trans_group_list_ = add_translator (trans_group_list_,t);
85 }
86
87
88 void
89 Translator_group::add_fresh_group_translator (Translator*t)
90 {
91   Translator_group*tg = dynamic_cast<Translator_group*> (t);
92   trans_group_list_ = add_translator (trans_group_list_,t); 
93   Context_def * td = unsmob_context_def (tg->definition_);
94
95   /*
96     this can not move before add_translator(), because \override
97     operations require that we are in the hierarchy.
98    */
99   td->apply_default_property_operations (tg);
100
101   t->initialize ();
102 }
103
104 bool
105 Translator_group::is_removable () const
106 {
107   return trans_group_list_ == SCM_EOL && ! iterator_count_;
108 }
109
110 Translator_group *
111 Translator_group::find_existing_translator (SCM n, String id)
112 {
113   if ((is_alias (n) && (id_string_ == id || id.is_empty ())) || n == ly_symbol2scm ("Current"))
114     return this;
115
116   Translator_group* r = 0;
117   for (SCM p = trans_group_list_; !r && gh_pair_p (p); p = ly_cdr (p))
118     {
119       Translator *  t = unsmob_translator (ly_car (p));
120       
121       r = dynamic_cast<Translator_group*> (t)->find_existing_translator (n, id);    }
122
123   return r;
124 }
125
126
127 Translator_group*
128 Translator_group::find_create_translator (SCM n, String id, SCM operations)
129 {
130   Translator_group * existing = find_existing_translator (n,id);
131   if (existing)
132     return existing;
133
134   Link_array<Context_def> path
135     = unsmob_context_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
136
137   if (path.size ())
138     {
139       Translator_group * current = this;
140
141       // start at 1.  The first one (index 0) will be us.
142       for (int i=0; i < path.size (); i++)
143         {
144           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
145
146           Translator_group * new_group
147             = path[i]->instantiate (output_def_, ops);
148
149           if (i == path.size () -1)
150             {
151               new_group->id_string_ = id;
152             }
153
154           current->add_fresh_group_translator (new_group);
155           apply_property_operations (new_group, ops);
156           
157           current = new_group;
158         }
159
160       return current;
161     }
162
163   Translator_group *ret = 0;
164   if (daddy_trans_)
165     ret = daddy_trans_->find_create_translator (n, id, operations);
166   else
167     {
168       warning (_f ("can't find or create `%s' called `%s'", ly_symbol2string (n).to_str0 (), id));
169       ret =0;
170     }
171   return ret;
172 }
173
174 bool
175 Translator_group::try_music (Music* m)
176 {
177   bool hebbes_b = try_music_on_nongroup_children (m);
178   
179   if (!hebbes_b && daddy_trans_)
180     hebbes_b = daddy_trans_->try_music (m);
181   
182   return hebbes_b ;
183 }
184
185 int
186 Translator_group::get_depth () const
187 {
188   return (daddy_trans_) ? daddy_trans_->get_depth ()  + 1 : 0;
189 }
190
191 Translator_group*
192 Translator_group::get_ancestor (int level)
193 {
194   if (!level || !daddy_trans_)
195     return this;
196
197   return daddy_trans_->get_ancestor (level-1);
198 }
199
200 void
201 Translator_group::terminate_translator (Translator*r)
202 {
203   r->finalize ();
204   /*
205     Return value ignored. GC does the rest.
206    */
207   remove_translator (r);
208 }
209
210
211 /**
212    Remove a translator from the hierarchy.
213  */
214 Translator *
215 Translator_group::remove_translator (Translator*trans)
216 {
217   assert (trans);
218
219   trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
220   trans->daddy_trans_ = 0;
221   return trans;
222 }
223
224 bool
225 Translator_group::is_bottom_translator_b () const
226 {
227   return !gh_symbol_p (unsmob_context_def (definition_)->default_child_context_name ());
228 }
229
230 Translator_group*
231 Translator_group::get_default_interpreter ()
232 {
233   if (!is_bottom_translator_b ())
234     {
235       SCM nm = unsmob_context_def (definition_)->default_child_context_name ();
236       SCM st = get_output_def ()->find_translator (nm);
237
238       Context_def *t = unsmob_context_def (st);
239       if (!t)
240         {
241           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
242           t = unsmob_context_def (this->definition_);
243         }
244       Translator_group *tg = t->instantiate (output_def_, SCM_EOL);
245       add_fresh_group_translator (tg);
246
247       if (!tg->is_bottom_translator_b ())
248         return tg->get_default_interpreter ();
249       else
250         return tg;
251     }
252   return this;
253 }
254
255 static void
256 static_each (SCM list, Method_pointer method)
257 {
258   for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
259     (unsmob_translator (ly_car (p))->*method) ();
260   
261 }
262
263 void
264 Translator_group::each (Method_pointer method) 
265 {
266   static_each (get_simple_trans_list (), method);
267   static_each (trans_group_list_, method);
268 }
269
270
271 /*
272   PROPERTIES
273  */
274 Translator_group*
275 Translator_group::where_defined (SCM sym) const
276 {
277   if (properties_dict ()->contains (sym))
278     {
279       return (Translator_group*)this;
280     }
281
282   return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
283 }
284
285 /*
286   return SCM_EOL when not found.
287 */
288 SCM
289 Translator_group::internal_get_property (SCM sym) const
290 {
291   SCM val =SCM_EOL;
292   if (properties_dict ()->try_retrieve (sym, &val))
293     return val;
294
295   if (daddy_trans_)
296     return daddy_trans_->internal_get_property (sym);
297   
298   return val;
299 }
300
301 void
302 Translator_group::internal_set_property (SCM sym, SCM val)
303 {
304 #ifndef NDEBUG
305   if (internal_type_checking_global_b)
306     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
307 #endif
308   
309   properties_dict ()->set (sym, val);
310 }
311
312 /*
313   TODO: look up to check whether we have inherited var? 
314  */
315 void
316 Translator_group::unset_property (SCM sym)
317 {
318   properties_dict ()->remove (sym);
319 }
320
321
322 /*
323   Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
324   entry from a translator property list by name of PROP
325 */
326 void
327 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
328 {
329   if (gh_symbol_p (prop))
330     {
331       if (val != SCM_UNDEFINED)
332         {
333           SCM prev = internal_get_property (prop);
334
335           if (gh_pair_p (prev) || prev == SCM_EOL)
336             {
337               bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
338               
339               if (ok)
340                 {
341                   prev = gh_cons (gh_cons (eltprop, val), prev);
342                   internal_set_property (prop, prev);
343                 }
344             }
345           else
346             {
347               // warning here.
348             }
349           
350         }
351       else
352         {
353           SCM prev = internal_get_property (prop);
354
355           /*
356             TODO: should have scm_equal_something () for reverting
357             autobeam properties.
358            */
359           SCM newprops= SCM_EOL ;
360           while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop))
361             {
362               newprops = gh_cons (ly_car (prev), newprops);
363               prev = ly_cdr (prev);
364             }
365           
366           if (gh_pair_p (prev))
367             {
368               newprops = scm_reverse_x (newprops, ly_cdr (prev));
369               internal_set_property (prop, newprops);
370             }
371         }
372     }
373 }
374
375
376
377 /*
378   STUBS
379 */
380 void
381 Translator_group::stop_translation_timestep ()
382 {
383   each (&Translator::stop_translation_timestep);
384 }
385
386 void
387 Translator_group::start_translation_timestep ()
388 {
389   each (&Translator::start_translation_timestep);
390 }
391
392 void
393 Translator_group::do_announces ()
394 {
395   each (&Translator::do_announces);
396 }
397
398 void
399 Translator_group::initialize ()
400 {
401   SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
402   set_property ("acceptHashTable", tab);
403   each (&Translator::initialize);
404 }
405
406 void
407 Translator_group::finalize ()
408 {
409   each (&Translator::finalize);
410 }
411
412 bool
413 translator_accepts_any_of (Translator*tr, SCM ifaces)
414 {
415   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
416                            tr->translator_description());
417   ack_ifs = gh_cdr (ack_ifs);
418   for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
419     if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
420       return true;
421   return false;
422 }
423
424 SCM
425 find_accept_translators (SCM gravlist, SCM ifaces)
426 {
427   SCM l = SCM_EOL;
428   for (SCM s = gravlist; ly_pair_p (s);  s = ly_cdr (s))
429     {
430       Translator* tr = unsmob_translator (ly_car (s));
431       if (translator_accepts_any_of (tr, ifaces))
432         l = scm_cons (tr->self_scm (), l); 
433     }
434   l = scm_reverse_x (l, SCM_EOL);
435
436   return l;
437 }
438
439 bool
440 Translator_group::try_music_on_nongroup_children (Music *m )
441 {
442   SCM tab = get_property ("acceptHashTable");
443   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
444                               m->get_property_alist (false));
445
446   if (!gh_pair_p (name))
447     return false;
448
449   name = gh_cdr (name);
450   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
451   if (accept_list == SCM_BOOL_F)
452     {
453       accept_list = find_accept_translators (get_simple_trans_list (),
454                                              m->get_mus_property ("types"));
455       scm_hashq_set_x (tab, name, accept_list);
456     }
457
458   for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
459     {
460       Translator * t = unsmob_translator (ly_car (p));
461       if (t && t->try_music (m))
462         return true;
463     }
464   return false;
465 }
466
467 SCM
468 Translator_group::properties_as_alist () const
469 {
470   return properties_dict()->to_alist();
471 }
472
473 String
474 Translator_group::context_name () const
475 {
476   Context_def * td = unsmob_context_def (definition_ );
477   return ly_symbol2string (td->get_context_name ());
478 }
479
480 /*
481   PRE_INIT_OPS is in the order specified, and hence must be reversed.
482  */
483 void
484 apply_property_operations (Translator_group*tg, SCM pre_init_ops)
485 {
486   SCM correct_order = scm_reverse (pre_init_ops);
487   for (SCM s = correct_order; gh_pair_p (s); s = ly_cdr (s))
488     {
489       SCM entry = ly_car (s);
490       SCM type = ly_car (entry);
491       entry = ly_cdr (entry); 
492       
493       if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush"))
494         {
495           SCM val = ly_cddr (entry);
496           val = gh_pair_p (val) ? ly_car (val) : SCM_UNDEFINED;
497
498           tg->execute_pushpop_property (ly_car (entry), ly_cadr (entry), val);
499         }
500       else if (type == ly_symbol2scm ("assign"))
501         {
502           tg->internal_set_property (ly_car (entry), ly_cadr (entry));
503         }
504     }
505 }
506
507 SCM
508 names_to_translators (SCM namelist, Translator_group*tg)
509 {
510   SCM l = SCM_EOL;
511   for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s))
512     {
513       Translator * t = get_translator (ly_car (s));
514       if (!t)
515         warning (_f ("can't find: `%s'", s));
516       else
517         {
518           Translator * tr = t->clone ();
519           SCM str = tr->self_scm ();
520           l = gh_cons (str, l);
521
522           tr->daddy_trans_ = tg;
523           tr->output_def_  = tg->output_def_;
524
525           scm_gc_unprotect_object (str);
526         }
527     }
528   return l;
529 }
530
531
532 SCM
533 Translator_group::get_simple_trans_list ()
534 {
535   return simple_trans_list_;
536
537 }
538
539
540   
541 #if 0
542 SCM
543 Translator_group::get_simple_trans_list ()
544 {
545   if (simple_trans_list_ != SCM_BOOL_F)
546     return simple_trans_list_;
547   
548   Context_def * td = unsmob_context_def (definition_);
549
550   /*
551     The following cannot work, since start_translation_timestep ()
552     triggers this code, and start_translation_timestep happens before
553     \property Voice.Voice =#'()
554     
555    */
556       trans_names = td->get_translator_names (SCM_EOL); 
557   
558   SCM trans_names = internal_get_property (td->get_context_name ());
559   if (!gh_pair_p (trans_names))
560     {
561     }
562
563   simple_trans_list_ = names_to_translators (trans_names, this);
564
565   
566   static_each (simple_trans_list_, &Translator::initialize);
567   return simple_trans_list_;
568 }
569 #endif