]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
* lily/stem.cc (thickness): new function.
[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
135   /*
136     TODO: use accepts_list_.
137    */
138   Link_array<Context_def> path
139     = unsmob_context_def (definition_)->path_to_acceptable_translator (n, get_output_def ());
140
141   if (path.size ())
142     {
143       Translator_group * current = this;
144
145       // start at 1.  The first one (index 0) will be us.
146       for (int i=0; i < path.size (); i++)
147         {
148           SCM ops = (i == path.size () -1) ? operations : SCM_EOL;
149
150           Translator_group * new_group
151             = path[i]->instantiate (output_def_, ops);
152
153           if (i == path.size () -1)
154             {
155               new_group->id_string_ = id;
156             }
157
158           current->add_fresh_group_translator (new_group);
159           apply_property_operations (new_group, ops);
160           
161           current = new_group;
162         }
163
164       return current;
165     }
166
167   Translator_group *ret = 0;
168   if (daddy_trans_)
169     ret = daddy_trans_->find_create_translator (n, id, operations);
170   else
171     {
172       warning (_f ("can't find or create `%s' called `%s'", ly_symbol2string (n).to_str0 (), id));
173       ret =0;
174     }
175   return ret;
176 }
177
178 bool
179 Translator_group::try_music (Music* m)
180 {
181   bool hebbes_b = try_music_on_nongroup_children (m);
182   
183   if (!hebbes_b && daddy_trans_)
184     hebbes_b = daddy_trans_->try_music (m);
185   
186   return hebbes_b ;
187 }
188
189 int
190 Translator_group::get_depth () const
191 {
192   return (daddy_trans_) ? daddy_trans_->get_depth ()  + 1 : 0;
193 }
194
195 void
196 Translator_group::terminate_translator (Translator*r)
197 {
198   r->finalize ();
199   /*
200     Return value ignored. GC does the rest.
201    */
202   remove_translator (r);
203 }
204
205
206 /**
207    Remove a translator from the hierarchy.
208  */
209 Translator *
210 Translator_group::remove_translator (Translator*trans)
211 {
212   assert (trans);
213
214   trans_group_list_ = scm_delq_x (trans->self_scm (), trans_group_list_);
215   trans->daddy_trans_ = 0;
216   return trans;
217 }
218
219
220 /*
221   Default child context as a SCM string, or something else if there is
222   none.
223 */
224 SCM
225 default_child_context_name (Translator_group const *tg)
226 {
227   return gh_pair_p (tg->accepts_list_)
228     ? ly_car (scm_last_pair (tg->accepts_list_))
229     : SCM_EOL;
230 }
231
232
233 bool
234 Translator_group::is_bottom_context () const
235 {
236   return !gh_symbol_p (default_child_context_name (this));
237 }
238
239 Translator_group*
240 Translator_group::get_default_interpreter ()
241 {
242   if (!is_bottom_context ())
243     {
244       SCM nm = default_child_context_name (this);
245       SCM st = get_output_def ()->find_translator (nm);
246
247       Context_def *t = unsmob_context_def (st);
248       if (!t)
249         {
250           warning (_f ("can't find or create: `%s'", ly_symbol2string (nm).to_str0 ()));
251           t = unsmob_context_def (this->definition_);
252         }
253       Translator_group *tg = t->instantiate (output_def_, SCM_EOL);
254       add_fresh_group_translator (tg);
255
256       if (!tg->is_bottom_context ())
257         return tg->get_default_interpreter ();
258       else
259         return tg;
260     }
261   return this;
262 }
263
264 static void
265 static_each (SCM list, Method_pointer method)
266 {
267   for (SCM p = list; gh_pair_p (p); p = ly_cdr (p))
268     (unsmob_translator (ly_car (p))->*method) ();
269   
270 }
271
272 void
273 Translator_group::each (Method_pointer method) 
274 {
275   static_each (get_simple_trans_list (), method);
276   static_each (trans_group_list_, method);
277 }
278
279
280 /*
281   PROPERTIES
282  */
283 Translator_group*
284 Translator_group::where_defined (SCM sym) const
285 {
286   if (properties_dict ()->contains (sym))
287     {
288       return (Translator_group*)this;
289     }
290
291   return (daddy_trans_) ? daddy_trans_->where_defined (sym) : 0;
292 }
293
294 /*
295   return SCM_EOL when not found.
296 */
297 SCM
298 Translator_group::internal_get_property (SCM sym) const
299 {
300   SCM val =SCM_EOL;
301   if (properties_dict ()->try_retrieve (sym, &val))
302     return val;
303
304   if (daddy_trans_)
305     return daddy_trans_->internal_get_property (sym);
306   
307   return val;
308 }
309
310 void
311 Translator_group::internal_set_property (SCM sym, SCM val)
312 {
313 #ifndef NDEBUG
314   if (internal_type_checking_global_b)
315     assert (type_check_assignment (sym, val, ly_symbol2scm ("translation-type?")));
316 #endif
317   
318   properties_dict ()->set (sym, val);
319 }
320
321 /*
322   TODO: look up to check whether we have inherited var? 
323  */
324 void
325 Translator_group::unset_property (SCM sym)
326 {
327   properties_dict ()->remove (sym);
328 }
329
330
331 /*
332   Push or pop (depending on value of VAL) a single entry (ELTPROP . VAL)
333   entry from a translator property list by name of PROP
334 */
335 void
336 Translator_group::execute_pushpop_property (SCM prop, SCM eltprop, SCM val)
337 {
338   if (gh_symbol_p (prop))
339     {
340       if (val != SCM_UNDEFINED)
341         {
342           SCM prev = internal_get_property (prop);
343
344           if (gh_pair_p (prev) || prev == SCM_EOL)
345             {
346               bool ok = type_check_assignment (eltprop, val, ly_symbol2scm ("backend-type?"));
347               
348               if (ok)
349                 {
350                   prev = gh_cons (gh_cons (eltprop, val), prev);
351                   internal_set_property (prop, prev);
352                 }
353             }
354           else
355             {
356               // warning here.
357             }
358           
359         }
360       else
361         {
362           SCM prev = internal_get_property (prop);
363
364           /*
365             TODO: should have scm_equal_something () for reverting
366             autobeam properties.
367            */
368           SCM newprops= SCM_EOL ;
369           while (gh_pair_p (prev) && !SCM_EQ_P(ly_caar (prev), eltprop))
370             {
371               newprops = gh_cons (ly_car (prev), newprops);
372               prev = ly_cdr (prev);
373             }
374           
375           if (gh_pair_p (prev))
376             {
377               newprops = scm_reverse_x (newprops, ly_cdr (prev));
378               internal_set_property (prop, newprops);
379             }
380         }
381     }
382 }
383
384
385
386 /*
387   STUBS
388 */
389 void
390 Translator_group::stop_translation_timestep ()
391 {
392   each (&Translator::stop_translation_timestep);
393 }
394
395 void
396 Translator_group::start_translation_timestep ()
397 {
398   each (&Translator::start_translation_timestep);
399 }
400
401 void
402 Translator_group::do_announces ()
403 {
404   each (&Translator::do_announces);
405 }
406
407 void
408 Translator_group::initialize ()
409 {
410   SCM tab = scm_make_vector (gh_int2scm (19), SCM_BOOL_F);
411   set_property ("acceptHashTable", tab);
412   each (&Translator::initialize);
413 }
414
415 void
416 Translator_group::finalize ()
417 {
418   each (&Translator::finalize);
419 }
420
421 bool
422 translator_accepts_any_of (Translator*tr, SCM ifaces)
423 {
424   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
425                            tr->translator_description());
426   ack_ifs = gh_cdr (ack_ifs);
427   for (SCM s = ifaces; ly_pair_p (s); s = ly_cdr (s))
428     if (scm_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
429       return true;
430   return false;
431 }
432
433 SCM
434 find_accept_translators (SCM gravlist, SCM ifaces)
435 {
436   SCM l = SCM_EOL;
437   for (SCM s = gravlist; ly_pair_p (s);  s = ly_cdr (s))
438     {
439       Translator* tr = unsmob_translator (ly_car (s));
440       if (translator_accepts_any_of (tr, ifaces))
441         l = scm_cons (tr->self_scm (), l); 
442     }
443   l = scm_reverse_x (l, SCM_EOL);
444
445   return l;
446 }
447
448 bool
449 Translator_group::try_music_on_nongroup_children (Music *m )
450 {
451   SCM tab = get_property ("acceptHashTable");
452   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
453                               m->get_property_alist (false));
454
455   if (!gh_pair_p (name))
456     return false;
457
458   name = gh_cdr (name);
459   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
460   if (accept_list == SCM_BOOL_F)
461     {
462       accept_list = find_accept_translators (get_simple_trans_list (),
463                                              m->get_mus_property ("types"));
464       scm_hashq_set_x (tab, name, accept_list);
465     }
466
467   for (SCM p = accept_list; gh_pair_p (p); p = ly_cdr (p))
468     {
469       Translator * t = unsmob_translator (ly_car (p));
470       if (t && t->try_music (m))
471         return true;
472     }
473   return false;
474 }
475
476 SCM
477 Translator_group::properties_as_alist () const
478 {
479   return properties_dict()->to_alist();
480 }
481
482 String
483 Translator_group::context_name () const
484 {
485   Context_def * td = unsmob_context_def (definition_ );
486   return ly_symbol2string (td->get_context_name ());
487 }
488
489 /*
490   PRE_INIT_OPS is in the order specified, and hence must be reversed.
491  */
492 void
493 apply_property_operations (Translator_group*tg, SCM pre_init_ops)
494 {
495   SCM correct_order = scm_reverse (pre_init_ops);
496   for (SCM s = correct_order; gh_pair_p (s); s = ly_cdr (s))
497     {
498       SCM entry = ly_car (s);
499       SCM type = ly_car (entry);
500       entry = ly_cdr (entry); 
501       
502       if (type == ly_symbol2scm ("push") || type == ly_symbol2scm ("poppush"))
503         {
504           SCM val = ly_cddr (entry);
505           val = gh_pair_p (val) ? ly_car (val) : SCM_UNDEFINED;
506
507           tg->execute_pushpop_property (ly_car (entry), ly_cadr (entry), val);
508         }
509       else if (type == ly_symbol2scm ("assign"))
510         {
511           tg->internal_set_property (ly_car (entry), ly_cadr (entry));
512         }
513     }
514 }
515
516 SCM
517 names_to_translators (SCM namelist, Translator_group*tg)
518 {
519   SCM l = SCM_EOL;
520   for (SCM s = namelist; gh_pair_p (s) ; s = ly_cdr (s))
521     {
522       Translator * t = get_translator (ly_car (s));
523       if (!t)
524         warning (_f ("can't find: `%s'", s));
525       else
526         {
527           Translator * tr = t->clone ();
528           SCM str = tr->self_scm ();
529           l = gh_cons (str, l);
530
531           tr->daddy_trans_ = tg;
532           tr->output_def_  = tg->output_def_;
533
534           scm_gc_unprotect_object (str);
535         }
536     }
537   return l;
538 }
539
540
541 SCM
542 Translator_group::get_simple_trans_list ()
543 {
544   return simple_trans_list_;
545
546 }
547
548
549   
550 #if 0
551 SCM
552 Translator_group::get_simple_trans_list ()
553 {
554   if (simple_trans_list_ != SCM_BOOL_F)
555     return simple_trans_list_;
556   
557   Context_def * td = unsmob_context_def (definition_);
558
559   /*
560     The following cannot work, since start_translation_timestep ()
561     triggers this code, and start_translation_timestep happens before
562     \property Voice.Voice =#'()
563     
564    */
565       trans_names = td->get_translator_names (SCM_EOL); 
566   
567   SCM trans_names = internal_get_property (td->get_context_name ());
568   if (!gh_pair_p (trans_names))
569     {
570     }
571
572   simple_trans_list_ = names_to_translators (trans_names, this);
573
574   
575   static_each (simple_trans_list_, &Translator::initialize);
576   return simple_trans_list_;
577 }
578 #endif