]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
3f00330e61336c875aa3c97d618157468e2befd7
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "score.hh"
10
11 #include <cstdio>
12
13 using namespace std;
14
15 #include "book.hh"
16 #include "cpu-timer.hh"
17 #include "global-context.hh"
18 #include "international.hh"
19 #include "lily-parser.hh"
20 #include "lilypond-key.hh"
21 #include "main.hh"
22 #include "music.hh"
23 #include "music.hh"
24 #include "output-def.hh"
25 #include "paper-book.hh"
26 #include "paper-score.hh"
27 #include "warn.hh"
28
29 #include "ly-smobs.icc"
30
31 Input *
32 Score::origin () const
33 {
34   return unsmob_input (input_location_);
35 }
36
37
38 Score::Score ()
39 {
40   header_ = SCM_EOL;
41   music_ = SCM_EOL;
42   error_found_ = false;
43   input_location_ = SCM_EOL;
44   smobify_self ();
45   input_location_ = make_input (Input ());
46 }
47
48 Score::~Score ()
49 {
50 }
51
52 IMPLEMENT_SMOBS (Score);
53 IMPLEMENT_DEFAULT_EQUAL_P (Score);
54 IMPLEMENT_TYPE_P (Score, "ly:score?");
55
56 SCM
57 Score::mark_smob (SCM s)
58 {
59   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
60
61   scm_gc_mark (sc->header_);
62   for (vsize i = sc->defs_.size (); i--;)
63     scm_gc_mark (sc->defs_[i]->self_scm ());
64
65   scm_gc_mark (sc->input_location_);
66   return sc->music_;
67 }
68
69 int
70 Score::print_smob (SCM, SCM p, scm_print_state*)
71 {
72   scm_puts ("#<Score>", p);
73
74   return 1;
75 }
76
77 Score::Score (Score const &s)
78 {
79   header_ = SCM_EOL;
80   music_ = SCM_EOL;
81   error_found_ = s.error_found_;
82   input_location_ = SCM_EOL;
83   smobify_self ();
84   input_location_ = make_input (*s.origin ()); 
85
86   Music *m = unsmob_music (s.music_);
87   if (m)
88     {
89       Music *mclone = m->clone ();
90       music_ = mclone->unprotect ();
91     }
92   else
93     music_ = SCM_EOL;
94
95   for (vsize i = 0, n = s.defs_.size (); i < n; i++)
96     {
97       Output_def *copy = s.defs_[i]->clone ();
98       defs_.push_back (copy);
99       copy->unprotect ();
100     }
101   header_ = ly_make_anonymous_module (false);
102   if (ly_is_module (s.header_))
103     ly_module_copy (header_, s.header_);
104 }
105
106 void
107 default_rendering (SCM music, SCM outdef,
108                    SCM book_outputdef,
109                    SCM header,
110                    SCM outname,
111                    SCM key)
112 {
113   SCM scaled_def = outdef;
114   SCM scaled_bookdef = book_outputdef;
115
116   Output_def *bpd = unsmob_output_def (book_outputdef);
117
118   /* ugh.  */
119   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
120     {
121       Real scale = scm_to_double (bpd->c_variable ("output-scale"));
122
123       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
124       Output_def *bdef = scale_output_def (bpd, scale);
125       def->parent_ = bdef;
126
127       scaled_def = def->self_scm ();
128       scaled_bookdef = bdef->self_scm ();
129
130       def->unprotect ();
131       bdef->unprotect ();
132     }
133
134   SCM context = ly_run_translator (music, scaled_def, key);
135   
136   SCM output_as_scm = ly_format_output (context);
137   Music_output *output = unsmob_music_output (output_as_scm);
138
139   if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
140     {
141       /* ugh, this is strange, Paper_book without a Book object. */
142       Paper_book *paper_book = new Paper_book ();
143       paper_book->header_ = header;
144       paper_book->paper_ = unsmob_output_def (scaled_bookdef);
145
146       if (ly_is_module (header))
147         paper_book->add_score (header);
148
149       paper_book->add_score (pscore->self_scm ());
150       paper_book->classic_output (outname);
151       paper_book->unprotect ();
152     }
153
154   scm_remember_upto_here_1 (scaled_def);
155   scm_remember_upto_here_1 (output_as_scm);
156   scm_remember_upto_here_1 (scaled_bookdef);
157 }
158
159 /*
160   Format score, return list of Music_output objects.
161
162   LAYOUTBOOK should be scaled already.
163 */
164 SCM
165 Score::book_rendering (Output_def *layoutbook,
166                        Output_def *default_def,
167                        Object_key *book_key)
168 {
169   if (error_found_)
170     return SCM_EOL;
171
172   SCM scaled_bookdef = SCM_EOL;
173   Real scale = 1.0;
174
175   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
176     scale = scm_to_double (layoutbook->c_variable ("output-scale"));
177
178   SCM outputs = SCM_EOL;
179   SCM *tail = &outputs;
180
181   int outdef_count = defs_.size ();
182
183   Object_key *key = new Lilypond_general_key (book_key, user_key_, 0);
184   SCM scm_key = key->unprotect ();
185
186   for (int i = 0; !i || i < outdef_count; i++)
187     {
188       Output_def *def = outdef_count ? defs_[i] : default_def;
189       SCM scaled = SCM_EOL;
190
191       if (def->c_variable ("is-layout") == SCM_BOOL_T)
192         {
193           def = scale_output_def (def, scale);
194           def->parent_ = layoutbook;
195
196           scaled = def->unprotect ();
197         }
198
199       /* TODO: fix or junk --no-layout.  */
200       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
201       if (dynamic_cast<Global_context *> (unsmob_context (context)))
202         {
203           SCM s = ly_format_output (context);
204
205           *tail = scm_cons (s, SCM_EOL);
206           tail = SCM_CDRLOC (*tail);
207         }
208
209       scm_remember_upto_here_1 (scaled);
210     }
211
212   scm_remember_upto_here_1 (scm_key);
213   scm_remember_upto_here_1 (scaled_bookdef);
214   return outputs;
215 }
216
217 void
218 Score::set_music (SCM music)
219 {
220   if (unsmob_music (music_))
221     {
222       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
223       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
224     }
225   Music *m = unsmob_music (music);
226   if (m && to_boolean (m->get_property ("error-found")))
227     {
228       m->origin ()->error (_ ("errors found, ignoring music expression"));
229
230       this->error_found_ = this->error_found_
231         || to_boolean (m->get_property ("error-found"));
232     }
233
234   if (this->error_found_)
235     this->music_ = SCM_EOL;
236   else
237     this->music_ = music;
238 }
239
240 SCM
241 Score::get_music () const
242 {
243   return music_;
244 }
245
246 void
247 Score::add_output_def (Output_def *def)
248 {
249   defs_.push_back (def);
250 }