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