]> git.donarmstrong.com Git - lilypond.git/blob - lily/translator-group.cc
* lily/text-item.cc (interpret_string): new file, select font with
[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--2004 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 "context-def.hh"
16 #include "context.hh"
17 #include "main.hh"
18 #include "music.hh"
19
20 Translator_group*
21 Translator_group::get_daddy_translator () const
22 {
23   Translator *t
24     = unsmob_translator (daddy_context_->daddy_context_->implementation_);
25   return dynamic_cast<Translator_group*> (t);
26 }
27
28
29 void
30 translator_each (SCM list, Translator_method method)
31 {
32   for (SCM p = list; is_pair (p); p = ly_cdr (p))
33     (unsmob_translator (ly_car (p))->*method) ();
34 }
35
36
37 void
38 Translator_group::initialize ()
39 {
40   SCM tab = scm_make_vector (scm_int2num (19), SCM_BOOL_F);
41   daddy_context_->set_property ("acceptHashTable", tab);
42 }
43
44
45 bool
46 translator_accepts_any_of (Translator*tr, SCM ifaces)
47 {
48   SCM ack_ifs = scm_assoc (ly_symbol2scm ("events-accepted"),
49                            tr->translator_description ());
50   ack_ifs = ly_cdr (ack_ifs);
51   for (SCM s = ifaces; is_pair (s); s = ly_cdr (s))
52     if (scm_c_memq (ly_car (s), ack_ifs) != SCM_BOOL_F)
53       return true;
54   return false;
55 }
56
57 SCM
58 find_accept_translators (SCM gravlist, SCM ifaces)
59 {
60   SCM l = SCM_EOL;
61   for (SCM s = gravlist; is_pair (s);  s = ly_cdr (s))
62     {
63       Translator* tr = unsmob_translator (ly_car (s));
64       if (translator_accepts_any_of (tr, ifaces))
65         l = scm_cons (tr->self_scm (), l); 
66     }
67   l = scm_reverse_x (l, SCM_EOL);
68
69   return l;
70 }
71
72 bool
73 Translator_group::try_music (Music* m)
74 {
75   SCM tab = get_property ("acceptHashTable");
76   SCM name = scm_sloppy_assq (ly_symbol2scm ("name"),
77                               m->get_property_alist (false));
78
79   if (!is_pair (name))
80     return false;
81
82   name = ly_cdr (name);
83   SCM accept_list = scm_hashq_ref (tab, name, SCM_UNDEFINED);
84   if (accept_list == SCM_BOOL_F)
85     {
86       accept_list = find_accept_translators (get_simple_trans_list (),
87                                              m->get_property ("types"));
88       scm_hashq_set_x (tab, name, accept_list);
89     }
90
91   for (SCM p = accept_list; is_pair (p); p = ly_cdr (p))
92     {
93       Translator * t = unsmob_translator (ly_car (p));
94       if (t && t->try_music (m))
95         return true;
96     }
97   return false;
98 }
99
100 SCM
101 names_to_translators (SCM namelist, Context*tg)
102 {
103   SCM l = SCM_EOL;
104   for (SCM s = namelist; is_pair (s) ; s = ly_cdr (s))
105     {
106       Translator * t = get_translator (ly_car (s));
107       if (!t)
108         warning (_f ("can't find: `%s'", s));
109       else
110         {
111           Translator * tr = t->clone ();
112           SCM str = tr->self_scm ();
113           l = scm_cons (str, l);
114
115           tr->daddy_context_ = tg;
116           scm_gc_unprotect_object (str);
117         }
118     }
119   return l;
120 }
121
122
123 SCM
124 Translator_group::get_simple_trans_list ()
125 {
126   return simple_trans_list_;
127 }
128
129 void
130 recurse_over_translators (Context * c, Translator_method ptr, Direction dir)
131 {
132   Translator_group * tg
133     = dynamic_cast<Translator_group*> (unsmob_translator (c->implementation_));
134
135
136   /*
137     Top down: 
138    */
139   if (dir == DOWN)
140     {
141       translator_each (tg->get_simple_trans_list (),
142                           ptr);
143
144       (tg->*ptr) ();
145     }
146
147   for (SCM s = c->context_list_ ; is_pair (s);
148        s =ly_cdr (s))
149     {
150       recurse_over_translators (unsmob_context (ly_car (s)), ptr, dir);
151     }
152
153   if (dir == UP)
154     {
155       translator_each (tg->get_simple_trans_list (),
156                      ptr);
157
158       (tg->*ptr) ();
159     }
160 }