]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
*** empty log message ***
[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 Score::Score ()
32   : Input ()
33 {
34   header_ = SCM_EOL;
35   music_ = SCM_EOL;
36   error_found_ = false;
37   smobify_self ();
38 }
39
40 Score::~Score ()
41 {
42 }
43
44 IMPLEMENT_SMOBS (Score);
45 IMPLEMENT_DEFAULT_EQUAL_P (Score);
46 IMPLEMENT_TYPE_P (Score, "ly:score?");
47
48 SCM
49 Score::mark_smob (SCM s)
50 {
51   Score *sc = (Score *) SCM_CELL_WORD_1 (s);
52
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 Score::Score (Score const &s)
68   : Input (s)
69 {
70   header_ = SCM_EOL;
71   music_ = SCM_EOL;
72   error_found_ = s.error_found_;
73   smobify_self ();
74
75   Music *m = unsmob_music (s.music_);
76   if (m)
77     {
78       Music *mclone = m->clone ();
79       music_ = mclone->unprotect ();
80     }
81   else
82     music_ = SCM_EOL;
83
84   for (int i = 0, n = s.defs_.size (); i < n; i++)
85     {
86       Output_def *copy = s.defs_[i]->clone ();
87       defs_.push (copy);
88       copy->unprotect ();
89     }
90   header_ = ly_make_anonymous_module (false);
91   if (ly_is_module (s.header_))
92     ly_module_copy (header_, s.header_);
93 }
94
95 void
96 default_rendering (SCM music, SCM outdef,
97                    SCM book_outputdef,
98                    SCM header,
99                    SCM outname,
100                    SCM key)
101 {
102   SCM scaled_def = outdef;
103   SCM scaled_bookdef = book_outputdef;
104
105   Output_def *bpd = unsmob_output_def (book_outputdef);
106
107   /* ugh.  */
108   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
109     {
110       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
111
112       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
113       Output_def *bdef = scale_output_def (bpd, scale);
114       def->parent_ = bdef;
115
116       scaled_def = def->self_scm ();
117       scaled_bookdef = bdef->self_scm ();
118
119       def->unprotect ();
120       bdef->unprotect ();
121     }
122
123   SCM context = ly_run_translator (music, scaled_def, key);
124   
125   SCM output_as_scm = ly_format_output (context);
126   Music_output *output = unsmob_music_output (output_as_scm);
127
128   if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
129     {
130       /* ugh, this is strange, Paper_book without a Book object. */
131       Paper_book *paper_book = new Paper_book ();
132       paper_book->header_ = header;
133       paper_book->paper_ = unsmob_output_def (scaled_bookdef);
134
135       if (ly_is_module (header))
136         paper_book->add_score (header);
137
138       SCM systems = pscore->get_paper_systems ();
139       paper_book->add_score (systems);
140
141       paper_book->classic_output (outname);
142       paper_book->unprotect ();
143     }
144
145   scm_remember_upto_here_1 (scaled_def);
146   scm_remember_upto_here_1 (output_as_scm);
147   scm_remember_upto_here_1 (scaled_bookdef);
148 }
149
150 /*
151   Format score, return list of Music_output objects.
152
153   LAYOUTBOOK should be scaled already.
154 */
155 SCM
156 Score::book_rendering (Output_def *layoutbook,
157                        Output_def *default_def,
158                        Object_key *book_key)
159 {
160   if (error_found_)
161     return SCM_EOL;
162
163   SCM scaled_bookdef = SCM_EOL;
164   Real scale = 1.0;
165
166   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
167     scale = scm_to_double (layoutbook->c_variable ("outputscale"));
168
169   SCM outputs = SCM_EOL;
170   SCM *tail = &outputs;
171
172   int outdef_count = defs_.size ();
173
174   Object_key *key = new Lilypond_general_key (book_key, user_key_, 0);
175   SCM scm_key = key->unprotect ();
176
177   for (int i = 0; !i || i < outdef_count; i++)
178     {
179       Output_def *def = outdef_count ? defs_[i] : default_def;
180       SCM scaled = SCM_EOL;
181
182       if (def->c_variable ("is-layout") == SCM_BOOL_T)
183         {
184           def = scale_output_def (def, scale);
185           def->parent_ = layoutbook;
186
187           scaled = def->unprotect ();
188         }
189
190       /* TODO: fix or junk --no-layout.  */
191       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
192       if (dynamic_cast<Global_context *> (unsmob_context (context)))
193         {
194           SCM s = ly_format_output (context);
195
196           *tail = scm_cons (s, SCM_EOL);
197           tail = SCM_CDRLOC (*tail);
198         }
199
200       scm_remember_upto_here_1 (scaled);
201     }
202
203   scm_remember_upto_here_1 (scm_key);
204   scm_remember_upto_here_1 (scaled_bookdef);
205   return outputs;
206 }
207
208 void
209 Score::set_music (SCM music)
210 {
211   if (unsmob_music (music_))
212     {
213       unsmob_music (music)->origin ()->error (_ ("already have music in score"));
214       unsmob_music (music_)->origin ()->error (_ ("this is the previous music"));
215     }
216   Music *m = unsmob_music (music);
217   if (m && to_boolean (m->get_property ("error-found")))
218     {
219       m->origin ()->error (_ ("errors found, ignoring music expression"));
220
221       this->error_found_ = this->error_found_
222         || to_boolean (m->get_property ("error-found"));
223     }
224
225   if (this->error_found_)
226     this->music_ = SCM_EOL;
227   else
228     this->music_ = music;
229 }
230
231 SCM
232 Score::get_music () const
233 {
234   return music_;
235 }
236
237 void
238 Score::add_output_def (Output_def *def)
239 {
240   defs_.push (def);
241 }