]> git.donarmstrong.com Git - lilypond.git/blob - lily/scheme-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / scheme-engraver.cc
1 /*
2   scheme-engraver.cc -- implement Scheme_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   Copyright (c) 2009--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "scheme-engraver.hh"
23
24 #include "grob.hh"
25
26 #include "translator.icc"
27
28 #include "scm-hash.hh"
29
30 Preinit_Scheme_engraver::Preinit_Scheme_engraver ()
31 {
32   initialize_function_ = SCM_EOL;
33   finalize_function_ = SCM_EOL;
34
35   interface_acknowledger_hash_.set (SCM_EOL, SCM_EOL);
36
37   per_instance_listeners_ = SCM_EOL;
38   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
39     precomputable_methods_[i] = SCM_UNDEFINED;
40 }
41
42 Scheme_engraver::~Scheme_engraver ()
43 {
44 }
45
46 // Extracts the value if callable, if not return SCM_UNDEFINED;
47 static SCM
48 callable (SCM symbol, SCM defn)
49 {
50   SCM val = ly_assoc_get (symbol, defn, SCM_BOOL_F);
51   return ly_is_procedure (val) ? val : SCM_UNDEFINED;
52 }
53
54 bool
55 Scheme_engraver::must_be_last () const
56 {
57   return must_be_last_;
58 }
59
60 void
61 Scheme_engraver::fetch_precomputable_methods (SCM ptrs[])
62 {
63   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
64     ptrs[i] = precomputable_methods_[i];
65 }
66
67 Scheme_engraver::Scheme_engraver (SCM definition, Context *c)
68   : Engraver (c)
69 {
70   precomputable_methods_[START_TRANSLATION_TIMESTEP]
71     = callable (ly_symbol2scm ("start-translation-timestep"), definition);
72   precomputable_methods_[STOP_TRANSLATION_TIMESTEP]
73     = callable (ly_symbol2scm ("stop-translation-timestep"), definition);
74   precomputable_methods_[PROCESS_MUSIC]
75     = callable (ly_symbol2scm ("process-music"), definition);
76   precomputable_methods_[PROCESS_ACKNOWLEDGED]
77     = callable (ly_symbol2scm ("process-acknowledged"), definition);
78   initialize_function_ = callable (ly_symbol2scm ("initialize"), definition);
79   finalize_function_ = callable (ly_symbol2scm ("finalize"), definition);
80
81   SCM p = ly_assoc_get (ly_symbol2scm ("listeners"), definition, SCM_EOL);
82   SCM listeners = SCM_EOL;
83
84   must_be_last_ = to_boolean (ly_assoc_get (ly_symbol2scm ("must-be-last"),
85                                             definition,
86                                             SCM_BOOL_F));
87
88   for (; scm_is_pair (p); p = scm_cdr (p))
89     {
90       SCM event_class = scm_caar (p);
91       SCM proc = scm_cdar (p);
92
93       if (!(ly_is_procedure (proc) && ly_is_symbol (event_class)))
94         continue;
95
96       // We should check the arity of the function?
97
98       // Record for later lookup.
99       listeners = scm_acons (event_class, proc, listeners);
100     }
101
102   SCM hash1 =
103     init_acknowledgers (ly_assoc_get (ly_symbol2scm ("acknowledgers"),
104                                       definition, SCM_EOL));
105   SCM hash2 =
106     init_acknowledgers (ly_assoc_get (ly_symbol2scm ("end-acknowledgers"),
107                                       definition, SCM_EOL));
108
109   per_instance_listeners_ = listeners;
110   interface_acknowledger_hash_.set (hash1, hash2);
111
112   // It's not defined whether Scheme_engraver::derived_mark is already
113   // active while the construction is underway, so we make sure we
114   // keep a version of everything on the stack that is not still
115   // covered by `definition'.
116
117   scm_remember_upto_here_2 (definition, listeners);
118   scm_remember_upto_here_2 (hash1, hash2);
119
120   // TODO: hook up description, props read/written, grobs created
121   // etc. to provide automatic documentation.
122 }
123
124 SCM
125 Scheme_engraver::init_acknowledgers (SCM alist)
126 {
127   SCM hash = Scheme_hash_table::make_smob ();
128   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
129     {
130       SCM iface = scm_caar (p);
131       SCM proc = scm_cdar (p);
132
133       if (!(ly_is_procedure (proc) && ly_is_symbol (iface)))
134         continue;
135
136       unsmob<Scheme_hash_table>(hash)->set (iface, proc);
137     }
138   return hash;
139 }
140
141 SCM
142 Scheme_engraver::get_listener_list () const
143 {
144   return per_instance_listeners_;
145 }
146
147 void
148 Scheme_engraver::initialize ()
149 {
150   if (!SCM_UNBNDP (initialize_function_))
151     scm_call_1 (initialize_function_, self_scm ());
152 }
153
154 void
155 Scheme_engraver::finalize ()
156 {
157   if (!SCM_UNBNDP (finalize_function_))
158     scm_call_1 (finalize_function_, self_scm ());
159 }
160
161 void
162 Scheme_engraver::derived_mark () const
163 {
164   for (int i = 0; i < TRANSLATOR_METHOD_PRECOMPUTE_COUNT; i++)
165     scm_gc_mark (precomputable_methods_[i]);
166
167   scm_gc_mark (initialize_function_);
168   scm_gc_mark (finalize_function_);
169   scm_gc_mark (per_instance_listeners_);
170   scm_gc_mark (interface_acknowledger_hash_[START]);
171   scm_gc_mark (interface_acknowledger_hash_[STOP]);
172 }