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