]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* lily/paper-outputter.cc (output_expr):
[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
26 #include "paper-book.hh"
27
28
29 /*
30   TODO: junkme.
31  */
32 Score::Score ()
33   : Input ()
34 {
35   header_ = SCM_EOL;
36   music_ = SCM_EOL;
37
38   smobify_self ();
39 }
40
41 Score::~Score ()
42 {
43   
44 }
45
46
47
48
49 IMPLEMENT_SMOBS (Score);
50 IMPLEMENT_DEFAULT_EQUAL_P (Score);
51
52
53 SCM
54 Score::mark_smob (SCM s)
55 {
56   Score * sc = (Score*) SCM_CELL_WORD_1 (s);
57
58   if (sc->header_)
59     scm_gc_mark (sc->header_);
60   for (int i = sc->defs_.size (); i--;)
61     scm_gc_mark (sc->defs_[i]->self_scm ());
62   
63   return sc->music_;
64 }
65
66 int
67 Score::print_smob (SCM , SCM p, scm_print_state*)
68 {
69   scm_puts ("#<Score>", p);
70
71   return 1;
72 }
73
74
75
76 /*
77   store point & click locations.
78   Global to save some time. (Sue us!)
79  */
80
81 Score::Score (Score const &s)
82   : Input (s)
83 {
84   music_ = SCM_EOL;
85   header_ = 0;
86   smobify_self ();
87
88   Music * m =unsmob_music (s.music_);
89   music_ =  m?m->clone ()->self_scm () : SCM_EOL;
90   scm_gc_unprotect_object (music_);
91   
92   for (int i = 0; i < s.defs_.size (); i++)
93     defs_.push (s.defs_[i]->clone ());
94
95   header_ = ly_make_anonymous_module ();
96   if (ly_module_p (s.header_))
97     ly_copy_module_variables (header_, s.header_);
98 }
99
100 LY_DEFINE (ly_run_translator, "ly:run-translator", 
101           2, 0, 0, (SCM mus, SCM output_def),
102            "Process @var{mus} according to @var{output_def}. "
103            "An interpretation context is set up, "
104            "and @var{mus} is interpreted with it.  "
105            "The context is returned in its final state.")
106 {
107   Music_output_def *odef = unsmob_music_output_def (output_def);
108   Music *music = unsmob_music (mus);
109
110   SCM_ASSERT_TYPE (music, mus, SCM_ARG1, __FUNCTION__, "Music");
111   SCM_ASSERT_TYPE (odef, output_def, SCM_ARG2, __FUNCTION__, "Output definition");
112   
113   Cpu_timer timer;
114   
115   Global_context * trans = new Global_context (odef);
116   
117   if (!trans)
118     {
119       programming_error ("no toplevel translator");
120       return SCM_BOOL_F;
121     }
122   progress_indication (_ ("Interpreting music..."));
123   
124   trans->final_mom_ = music->get_length ();
125   SCM protected_iter = Music_iterator::get_static_get_iterator (music);
126   Music_iterator * iter = unsmob_iterator (protected_iter);
127   iter->init_translator (music, trans);
128
129   iter->construct_children ();
130
131   if (! iter->ok ())
132     {
133       warning (_ ("Need music in a score"));
134       return SCM_BOOL_F;        // todo: shoudl throw exception.
135     }
136
137   trans->run_iterator_on_me (iter);
138   iter->quit ();
139   scm_remember_upto_here_1 (protected_iter);
140   trans->finish ();
141
142   if (verbose_global_b)
143     progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
144
145   
146   return scm_gc_unprotect_object (trans->self_scm ());
147 }
148
149
150 LY_DEFINE (ly_render_output, "ly:render-output",
151            3, 0, 0, (SCM context, SCM header, SCM out_filename),
152            "Given a Score context in its final state, calculate the output, "
153            "and  dump the result to @var{out-filename}, using "
154            "@var{header} for the bibliographic information.")
155 {
156   Global_context * gt = dynamic_cast<Global_context *> (unsmob_context (context));
157   
158   SCM_ASSERT_TYPE (gt, context, SCM_ARG1, __FUNCTION__, "Global context");
159   SCM_ASSERT_TYPE (ly_module_p (header), header, SCM_ARG2, __FUNCTION__, "module");
160   SCM_ASSERT_TYPE (gh_string_p (out_filename), out_filename, SCM_ARG3, __FUNCTION__, "output filename");
161
162   Music_output *output = gt->get_output ();
163   output->header_ = header;
164   progress_indication ("\n");
165   output->process (ly_scm2string (out_filename));
166
167 #ifndef PAGE_LAYOUT
168   delete output;
169 #endif
170
171   return SCM_UNDEFINED;
172 }
173
174 void
175 default_rendering (SCM mus, SCM outdef, SCM head, SCM outname)
176 {
177   SCM context = ly_run_translator (mus, outdef);
178   
179   if (unsmob_context (context))
180     ly_render_output (context,  head, outname);
181 }