]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
(set-paper-dimension-variables): new
[lilypond.git] / lily / score.cc
1 /*
2   score.cc -- implement Score
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 <stdio.h>
10
11 #include "lily-parser.hh"
12 #include "book.hh"
13 #include "cpu-timer.hh"
14 #include "global-context.hh"
15 #include "ly-module.hh"
16 #include "ly-smobs.icc"
17 #include "main.hh"
18 #include "music-iterator.hh"
19 #include "output-def.hh"
20 #include "music-output.hh"
21 #include "music.hh"
22 #include "paper-book.hh"
23 #include "output-def.hh"
24 #include "paper-score.hh"
25 #include "scm-hash.hh"
26 #include "score.hh"
27 #include "warn.hh"
28
29 Score::Score ()
30   : Input ()
31 {
32   header_ = SCM_EOL;
33   music_ = SCM_EOL;
34   error_found_ = false;
35   smobify_self ();
36 }
37
38 Score::~Score ()
39 {
40 }
41
42 IMPLEMENT_SMOBS (Score);
43 IMPLEMENT_DEFAULT_EQUAL_P (Score);
44 IMPLEMENT_TYPE_P (Score, "ly:score?");
45
46 SCM
47 Score::mark_smob (SCM s)
48 {
49   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
50   if (sc->header_)
51     scm_gc_mark (sc->header_);
52   for (int i = sc->defs_.size (); i--;)
53     scm_gc_mark (sc->defs_[i]->self_scm ());
54   return sc->music_;
55 }
56
57 int
58 Score::print_smob (SCM , SCM p, scm_print_state*)
59 {
60   scm_puts ("#<Score>", p);
61
62   return 1;
63 }
64
65 Score::Score (Score const &s)
66   : Input (s)
67 {
68   music_ = SCM_EOL;
69   error_found_ = s.error_found_;
70   
71   /* FIXME: SCM_EOL? */
72   header_ = 0;
73
74   smobify_self ();
75
76   Music *m =unsmob_music (s.music_);
77   music_ = m ? m->clone ()->self_scm () : SCM_EOL;
78   scm_gc_unprotect_object (music_);
79   
80   for (int i = 0; i < s.defs_.size (); i++)
81     defs_.push (s.defs_[i]->clone ());
82
83   header_ = ly_make_anonymous_module (false);
84   if (ly_c_module_p (s.header_))
85     ly_import_module (header_, s.header_);
86 }
87
88
89 LY_DEFINE (ly_run_translator, "ly:run-translator", 
90            2, 0, 0, (SCM mus, SCM output_def),
91            "Process @var{mus} according to @var{output_def}. "
92            "An interpretation context is set up, "
93            "and @var{mus} is interpreted with it.  "
94            "The context is returned in its final state.")
95 {
96   Output_def *odef = unsmob_output_def (output_def);
97   Music *music = unsmob_music (mus);
98
99   if (!music
100       || !music->get_length ().to_bool ())
101     {
102       warning (_ ("Need music in a score"));
103       return SCM_BOOL_F;
104     }
105   
106   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
107   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
108   
109   Cpu_timer timer;
110   
111   Global_context *trans = new Global_context (odef, music->get_length ());
112   
113   if (!trans)
114     {
115       programming_error ("no toplevel translator");
116       return SCM_BOOL_F;
117     }
118   progress_indication (_ ("Interpreting music... "));
119   
120   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
121   Music_iterator * iter = unsmob_iterator (protected_iter);
122   iter->init_translator (music, trans);
123
124   iter->construct_children ();
125
126   if (!iter->ok ())
127     {
128       warning (_ ("Need music in a score"));
129       /* todo: should throw exception. */
130       return SCM_BOOL_F;
131     }
132
133   trans->run_iterator_on_me (iter);
134   iter->quit ();
135   scm_remember_upto_here_1 (protected_iter);
136   trans->finish ();
137
138   if (verbose_global_b)
139     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
140   
141   return scm_gc_unprotect_object (trans->self_scm ());
142 }
143
144 LY_DEFINE (ly_format_output, "ly:format-output",
145            2, 0, 0, (SCM context, SCM outname),
146            "Given a Score context in its final state,"
147            "process it and return the (rendered) result.")
148 {
149   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
150   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
151   SCM_ASSERT_TYPE (scm_is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
152
153   Music_output *output = g->get_output ();
154   progress_indication ("\n");
155   /* ugh, midi still wants outname  */
156   return output->process (ly_scm2string (outname));
157 }
158
159 void
160 default_rendering (SCM music, SCM outdef,
161                    SCM book_outputdef,
162                    SCM header, SCM outname)
163 {
164   SCM scaled_def = outdef;
165   SCM scaled_bookdef = book_outputdef;
166   
167   Output_def *bpd = unsmob_output_def (book_outputdef);
168
169   /* ugh.  */
170   if (bpd->c_variable ("is-bookpaper") == SCM_BOOL_T)
171     {
172       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
173       
174       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
175       scaled_def = def->self_scm ();
176
177       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
178       unsmob_output_def (scaled_def)->parent_
179         = unsmob_output_def (scaled_bookdef);
180       
181       scm_gc_unprotect_object (scaled_bookdef);
182       scm_gc_unprotect_object (scaled_def);
183     }
184   
185   SCM context = ly_run_translator (music, scaled_def);
186   if (Global_context *g = dynamic_cast<Global_context*>
187       (unsmob_context (context)))
188     {
189       SCM systems = ly_format_output (context, outname);
190       Music_output *output = g->get_output ();
191       if (systems != SCM_UNDEFINED)
192         {
193           /* ugh, this is strange, Paper_book without a Book object. */
194           Paper_book *paper_book = new Paper_book ();
195           paper_book->header_ = header;
196           paper_book->bookpaper_ = unsmob_output_def (scaled_bookdef);
197           
198           Score_systems sc;
199           sc.systems_ = systems;
200           sc.header_ = header;
201
202           paper_book->score_systems_.push (sc);
203           
204           paper_book->classic_output (ly_scm2string (outname));
205           scm_gc_unprotect_object (paper_book->self_scm ());
206         }
207       delete output;
208     }
209
210   scm_remember_upto_here_1 (scaled_def);
211   scm_remember_upto_here_1 (scaled_bookdef);
212 }
213
214 /*
215 Format score, return systems. OUTNAME is still passed to create a midi
216 file.
217
218 PAPERBOOK should be scaled already.
219
220 */
221 SCM
222 Score::book_rendering (String outname,
223                        Output_def *paperbook,
224                        Output_def *default_def)
225 {
226   if (error_found_)
227     return SCM_EOL;
228    
229   SCM scaled_bookdef = SCM_EOL;
230   Real scale = 1.0;
231
232   if (paperbook && paperbook->c_variable ("is-bookpaper") == SCM_BOOL_T)
233     scale = scm_to_double (paperbook->c_variable ("outputscale"));
234   
235   SCM out = scm_makfrom0str (outname.to_str0 ());
236   SCM systems = SCM_EOL;
237   int outdef_count = defs_.size ();
238   for (int i = 0; !i || i < outdef_count; i++)
239     {
240       Output_def *def = outdef_count ? defs_[i] : default_def;
241       SCM scaled = SCM_EOL;
242       if (def->c_variable ("is-paper") == SCM_BOOL_T)
243         {
244           def = scale_output_def (def, scale);
245           def->parent_ = paperbook;
246           scaled = def->self_scm ();
247           scm_gc_unprotect_object (scaled);
248         }
249
250       /* TODO: fix or junk --no-paper.  */
251       SCM context = ly_run_translator (music_, def->self_scm ());
252       if (dynamic_cast<Global_context*> (unsmob_context (context)))
253         {
254           SCM s = ly_format_output (context, out);
255           if (s != SCM_UNDEFINED)
256             systems = s;
257         }
258
259       scm_remember_upto_here_1 (scaled);
260     }
261   
262   scm_remember_upto_here_1 (scaled_bookdef);
263   return systems;
264 }
265
266
267
268
269 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
270            2, 0, 0, (SCM score, SCM paper),
271            "Run @var{score} through @var{paper}, an output definition, "
272            "scaled to correct outputscale already, "
273            "return a list of paper-lines.")
274 {
275   Score * sc = unsmob_score (score);
276   Output_def *od = unsmob_output_def (paper);
277
278   if (sc->error_found_)
279     {
280       return SCM_EOL;
281     }
282   
283   SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "Score");
284   SCM_ASSERT_TYPE (od, paper, SCM_ARG2, __FUNCTION__, "Output_def");
285
286   Output_def * score_def  = 0;
287
288   /* UGR, FIXME, these are default \paper blocks once again.  They
289      suck. */
290   for (int i = 0; !score_def && i < sc->defs_.size (); i++)
291     if (sc->defs_[i]->c_variable ("is-paper") == SCM_BOOL_T)
292       score_def = sc->defs_[i];
293
294   if (!score_def)
295     return scm_c_make_vector (0, SCM_EOL);
296       
297   score_def = score_def->clone ();
298   SCM prot = score_def->self_scm ();
299   scm_gc_unprotect_object (prot);
300
301   /* TODO: SCORE_DEF should be scaled according to OD->parent_ or OD
302      itself. */
303   score_def->parent_ = od;
304   
305   SCM context = ly_run_translator (sc->get_music (), score_def->self_scm ());
306   SCM lines = ly_format_output (context, scm_makfrom0str ("<embedded>"));
307   
308   scm_remember_upto_here_1 (prot);
309   return lines;
310 }
311
312 void
313 Score::set_music (SCM music, SCM parser)
314 {
315   /* URG? */
316   SCM check_funcs = ly_scheme_function ("toplevel-music-functions");
317   for (; scm_is_pair (check_funcs); check_funcs = scm_cdr (check_funcs))
318     music = scm_call_2 (scm_car (check_funcs), music, parser);
319
320   if (unsmob_music (music_))
321     {
322       unsmob_music (music)->origin ()->error (_("Already have music in score"));
323       unsmob_music (music_)->origin ()->error (_("This is the previous music"));
324     }
325   Music * m = unsmob_music (music);
326   if (m && to_boolean (m->get_property ("error-found")))
327     {
328       m->origin()->error (_("Error found in this music expression. Ignoring it"));
329       
330       this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
331       
332     }
333
334   if (this->error_found_)
335     this->music_ = SCM_EOL; 
336   else
337     this->music_ = music;
338
339 }
340
341 SCM
342 Score::get_music () const
343 {
344   return music_;
345 }