]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* lily/my-lily-parser.cc: remove paper_description function
[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 "ly-smobs.icc"
12
13 #include "score.hh"
14 #include "warn.hh"
15 #include "music-output-def.hh"
16 #include "music-output.hh"
17 #include "music-iterator.hh"
18 #include "music.hh"
19 #include "global-context.hh"
20 #include "scm-hash.hh"
21 #include "cpu-timer.hh"
22 #include "main.hh"
23 #include "paper-def.hh"
24 #include "ly-module.hh"
25 #include "paper-book.hh"
26 #include "paper-score.hh"
27 #include "input-file-results.hh"
28
29
30 /*
31   TODO: junkme.
32  */
33 Score::Score ()
34   : Input ()
35 {
36   header_ = SCM_EOL;
37   music_ = SCM_EOL;
38   smobify_self ();
39 }
40
41 Score::~Score ()
42 {
43 }
44
45 IMPLEMENT_SMOBS (Score);
46 IMPLEMENT_DEFAULT_EQUAL_P (Score);
47
48 SCM
49 Score::mark_smob (SCM s)
50 {
51   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
52   if (sc->header_)
53     scm_gc_mark (sc->header_);
54   for (int i = sc->defs_.size (); i--;)
55     scm_gc_mark (sc->defs_[i]->self_scm ());
56   return sc->music_;
57 }
58
59 int
60 Score::print_smob (SCM , SCM p, scm_print_state*)
61 {
62   scm_puts ("#<Score>", p);
63
64   return 1;
65 }
66
67
68
69 /*
70   store point & click locations.
71   Global to save some time. (Sue us!)
72  */
73
74 Score::Score (Score const &s)
75   : Input (s)
76 {
77   music_ = SCM_EOL;
78
79   // FIXME: SCM_EOL?
80   header_ = 0;
81
82   smobify_self ();
83
84   Music * m =unsmob_music (s.music_);
85   music_ =  m?m->clone ()->self_scm () : SCM_EOL;
86   scm_gc_unprotect_object (music_);
87   
88   for (int i = 0; i < s.defs_.size (); i++)
89     defs_.push (s.defs_[i]->clone ());
90
91   header_ = ly_make_anonymous_module ();
92   if (is_module (s.header_))
93     ly_import_module (header_, s.header_);
94 }
95
96 LY_DEFINE (ly_run_translator, "ly:run-translator", 
97           2, 0, 0, (SCM mus, SCM output_def),
98            "Process @var{mus} according to @var{output_def}. "
99            "An interpretation context is set up, "
100            "and @var{mus} is interpreted with it.  "
101            "The context is returned in its final state.")
102 {
103   Music_output_def *odef = unsmob_music_output_def (output_def);
104   Music *music = unsmob_music (mus);
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,
112                                                music->get_length ()
113                                                );
114   
115   if (!trans)
116     {
117       programming_error ("no toplevel translator");
118       return SCM_BOOL_F;
119     }
120   progress_indication (_ ("Interpreting music... "));
121   
122   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
123   Music_iterator * iter = unsmob_iterator (protected_iter);
124   iter->init_translator (music, trans);
125
126   iter->construct_children ();
127
128   if (! iter->ok ())
129     {
130       warning (_ ("Need music in a score"));
131       return SCM_BOOL_F;        // todo: should throw exception.
132     }
133
134   trans->run_iterator_on_me (iter);
135   iter->quit ();
136   scm_remember_upto_here_1 (protected_iter);
137   trans->finish ();
138
139   if (verbose_global_b)
140     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
141
142   
143   return scm_gc_unprotect_object (trans->self_scm ());
144 }
145
146 LY_DEFINE (ly_format_output, "ly:format-output",
147            2, 0, 0, (SCM context, SCM outname),
148            "Given a Score context in its final state,"
149            "process it and return the (rendered) result.")
150 {
151   Global_context *g = dynamic_cast<Global_context*> (unsmob_context (context));
152   SCM_ASSERT_TYPE (g, context, SCM_ARG1, __FUNCTION__, "Global context");
153   SCM_ASSERT_TYPE (is_string (outname), outname, SCM_ARG2, __FUNCTION__, "output filename");
154
155   Music_output *output = g->get_output ();
156   progress_indication ("\n");
157   // ugh, midi still wants outname
158   return output->process (ly_scm2string (outname));
159 }
160
161 void
162 default_rendering (SCM music, SCM outdef, SCM header, SCM outname)
163 {
164   SCM context = ly_run_translator (music, outdef);
165
166   if (Global_context *g = dynamic_cast<Global_context*>
167       (unsmob_context (context)))
168     {
169       SCM systems = ly_format_output (context, outname);
170       Music_output *output = g->get_output ();
171       if (systems != SCM_UNDEFINED)
172         {
173           Paper_book *paper_book = new Paper_book ();
174           Paper_score *ps = dynamic_cast<Paper_score*> (output);
175           paper_book->papers_.push (ps->paper_);
176           paper_book->scores_.push (systems);
177
178           // fixme.
179           //paper_book->global_headers_.push (global_input_file->header_);
180           
181           paper_book->headers_.push (header);
182           paper_book->classic_output (ly_scm2string (outname));
183           scm_gc_unprotect_object (paper_book->self_scm ());
184         }
185       delete output;
186     }
187 }
188
189 SCM
190 Score::book_rendering (String outname, Music_output_def *default_def,
191                        Paper_def **paper)
192 {
193   SCM out = scm_makfrom0str (outname.to_str0 ());
194   SCM systems = SCM_EOL;
195   int outdef_count = defs_.size ();
196   for (int i = 0; !i || i < outdef_count; i++)
197     {
198       Music_output_def *def = outdef_count ? defs_[i] : default_def;
199       SCM context = ly_run_translator (music_, def->self_scm ());
200       if (Global_context *g = dynamic_cast<Global_context*>
201           (unsmob_context (context)))
202         {
203           SCM s = ly_format_output (context, out);
204           if (s != SCM_UNDEFINED)
205             {
206               systems = s;
207               /* Ugh. */
208               Music_output *output = g->get_output ();
209               if (Paper_score *ps = dynamic_cast<Paper_score*> (output))
210                 *paper = ps->paper_;
211             }
212         }
213     }
214   return systems;
215 }