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