]> git.donarmstrong.com Git - lilypond.git/blob - lily/global-context.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[lilypond.git] / lily / global-context.cc
1 /*
2   global-translator.cc -- implement global_context
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 "global-context.hh"
10
11 #include <cstdio>
12
13 #include "warn.hh"
14 #include "event.hh"
15 #include "music-list.hh"
16 #include "music-iterator.hh"
17 #include "score-context.hh"
18 #include "context-def.hh"
19 #include "output-def.hh"
20 #include "lilypond-key.hh"
21
22 Global_context::Global_context (Output_def *o, Moment final, Object_key *key)
23   : Context (new Lilypond_context_key(key,
24                                       Moment(0),
25                                       "Global", "", 0))
26 {
27   scm_gc_unprotect_object (key_->self_scm());
28   
29   output_def_ = o;
30   final_mom_ = final;
31   definition_ = find_context_def (o, ly_symbol2scm ("Global"));
32
33   Context_def *globaldef  =  unsmob_context_def (definition_);
34   if (!globaldef)
35     {
36       programming_error ("No `Global' context found.");
37     }
38   else
39     globaldef->apply_default_property_operations (this);
40   accepts_list_ = scm_list_1 (ly_symbol2scm ("Score"));
41 }
42
43 Output_def* 
44 Global_context::get_output_def () const
45 {
46   return output_def_;
47 }
48
49 void
50 Global_context::add_moment_to_process (Moment m)
51 {
52   if (m > final_mom_)
53     return;
54
55   if (m < now_mom_)
56     programming_error ("Trying to freeze in time.");
57   
58   for (int i=0; i <  extra_mom_pq_.size (); i++)
59     if (extra_mom_pq_[i] == m)
60       return;
61   extra_mom_pq_.insert (m);
62 }
63
64 Moment
65 Global_context::sneaky_insert_extra_moment (Moment w)
66 {
67   while (extra_mom_pq_.size () && extra_mom_pq_.front () <= w)
68     w = extra_mom_pq_.get ();
69   return w;
70 }
71
72 int
73 Global_context::get_moments_left () const
74 {
75   return extra_mom_pq_.size ();
76 }
77
78 void
79 Global_context::prepare (Moment m)
80 {
81   prev_mom_  = now_mom_;
82   now_mom_ = m;
83
84   clear_key_disambiguations ();
85   if (get_score_context ())
86     get_score_context ()->prepare (m);
87   
88 }
89
90 Moment
91 Global_context::now_mom () const
92 {
93   return now_mom_;
94 }
95
96 Score_context*
97 Global_context::get_score_context () const
98 {
99   return (scm_is_pair (context_list_))
100     ? dynamic_cast<Score_context*> (unsmob_context (scm_car (context_list_)))
101     : 0;
102 }
103
104 Music_output*
105 Global_context::get_output ()
106 {
107   return get_score_context ()->get_output ();
108 }
109
110 void
111 Global_context::one_time_step ()
112 {
113   get_score_context ()->one_time_step ();
114   apply_finalizations ();
115   check_removal ();
116 }
117
118 void
119 Global_context::finish ()
120 {
121   if (get_score_context ())
122     get_score_context ()->finish ();
123 }
124
125 void
126 Global_context::run_iterator_on_me (Music_iterator * iter)
127 {
128   if (iter-> ok ())
129     prev_mom_ = now_mom_ = iter->pending_moment ();
130
131   bool first = true;
132   while (iter->ok () || get_moments_left ())
133     {
134       Moment w;
135       w.set_infinite (1);
136       if (iter->ok ())
137         {
138           w = iter->pending_moment ();
139         }
140
141       w = sneaky_insert_extra_moment (w);
142       if (w.main_part_.is_infinity ())
143         break ;
144       
145       if (first)
146         {
147           /*
148             Need this to get grace notes at start of a piece correct.
149            */
150           first = false;
151           set_property ("measurePosition", w.smobbed_copy ());
152         }
153
154
155       prepare (w);
156
157       if (iter->ok ())
158         iter->process (w);
159
160       if (!get_score_context ()) 
161         {
162           SCM sym = ly_symbol2scm ("Score");
163           Context_def * t = unsmob_context_def (find_context_def (get_output_def (), sym));
164           if (!t)
165             error (_f ("can't find `%s' context", "Score"));
166
167           Object_key const *key = get_context_key ("Score", "");
168           Context *c = t->instantiate (SCM_EOL, key);
169           add_context (c);
170           scm_gc_unprotect_object (key->self_scm());
171
172           Score_context *sc = dynamic_cast<Score_context*> (c);
173           sc->prepare (w);
174         }
175       
176       one_time_step ();
177     }
178 }
179
180 void
181 Global_context::apply_finalizations ()
182 {
183   SCM lst = get_property ("finalizations");
184   set_property ("finalizations", SCM_EOL);
185   for (SCM s = lst; scm_is_pair (s); s = scm_cdr (s))
186     /* TODO: make safe.  */
187     scm_primitive_eval (scm_car (s));
188 }
189
190 /* Add a function to execute before stepping to the next time step.  */
191 void
192 Global_context::add_finalization (SCM x)
193 {
194   SCM lst = get_property ("finalizations");
195   lst = scm_cons (x, lst);
196   set_property ("finalizations", lst); 
197 }
198
199 Moment
200 Global_context::previous_moment () const
201 {
202   return prev_mom_;
203 }
204
205 Context *
206 Global_context::get_default_interpreter ()
207 {
208   if (get_score_context ())
209     return get_score_context ()->get_default_interpreter ();
210   else
211     return Context::get_default_interpreter ();
212 }