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